这是我的代码:
<html>
<head>
<script type="text/javascript" language="javascript">
function load() {
div1 = document.getElementById('div1');
div2 = document.getElementById('div2');
function clicky() {
alert('1');
}
div2.addEventListener('click', clicky);
//div1.innerHTML += 'ooooooofdsfdsfdsfdsfdsfsdfdsfdsfdsfdsklfdsjfkldsjfkldsjfldsjfkldsjfkldsjfdskl';
}
</script>
</head>
<body onload="load();">
<div id="div1" style="width : 300px; height : 300px; background-color : red; position : absolute">
<div id="div2" style="width : 20px; height : 20px; background-color : green; position : absolute; right : 5px; top : 5px;">X</div>
</div>
</body>
所以,我在 div1 中有 div2。如果评论的字符串真的被评论了,一切正常:点击 div2 会引起警报。但是,如果我将 smth 添加到 div1.innerHTML,事件处理程序将不起作用。哪里有问题?