Win*_*ker 14 javascript dom createelement
我正在尝试使用javascript来创建一个按钮,该按钮具有一个onclick事件,该事件调用头部中定义的函数,该函数作为相对于按钮的dom对象的参数.我该怎么做呢?
例如:
<html>
<head> <script>function blah(obj){alert(obj.value)}</script></head>
<body>
<button onclick="blah(this.parentNode.value);"></button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
var newButton = document.createElement("button");
???
Run Code Online (Sandbox Code Playgroud)
最后我希望新按钮与现有按钮相同.
Ala*_*ter 35
function createButton(context, func) {
var button = document.createElement("input");
button.type = "button";
button.value = "im a button";
button.onclick = func;
context.appendChild(button);
}
window.onload = function() {
createButton(document.body, function() {
highlight(this.parentNode.childNodes[1]);
// Example of different context, copied function etc
// createButton(this.parentNode, this.onclick);
});
};
Run Code Online (Sandbox Code Playgroud)
那是你要的吗?
归档时间: |
|
查看次数: |
72596 次 |
最近记录: |