In the following example code, I attach an onclick
event handler to the span containing the text "foo".(在以下示例代码中,我将onclick
事件处理程序附加到包含文本“ foo”的范围。)
The handler is an anonymous function that pops up an alert()
.(该处理程序是一个匿名函数,会弹出alert()
。)
However, if I assign to the parent node's innerHTML
, this onclick
event handler gets destroyed - clicking "foo" fails to pop up the alert box.(但是,如果我将其分配给父节点的innerHTML
,则此onclick
事件处理程序将被销毁-单击“ foo”将无法弹出警报框。)
Is this fixable?(这个可以解决吗?)
<html>
<head>
<script type="text/javascript">
function start () {
myspan = document.getElementById("myspan");
myspan.onclick = function() { alert ("hi"); };
mydiv = document.getElementById("mydiv");
mydiv.innerHTML += "bar";
}
</script>
</head>
<body onload="start()">
<div id="mydiv" style="border: solid red 2px">
<span id="myspan">foo</span>
</div>
</body>
</html>
ask by mike translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…