js 动态修改div的onclick属性

某个大的div:frameDiv
包含几个小的div:



动态设置frameShow1的onclick属性:document.getElementById('frameShow1').onclick=function(){alert("hahaha");}
但是,frameDiv的innerHTML打印出来,没有了onclick属性,请问这是为什么呢?谢谢啦~~~~~~~~

[code="html"]
onclick='alert('1');'
[/code]
我试过了,这个写法在Firefox , Chrome上都会在alert节点的innerHTML时,包含“onclick”部分,而在IE 6(我电脑上只有IE 6)上显示时,后面会有个“?”。如果把它改成:
[code="html"]
onclick="alert('1');"
[/code]
Firefox,Chrome上还是能正确显示,而IE 6上这不会显示onclick部分。我想这是浏览器之间实现的差异问题。

[quote]
[code="html"]

[/code]
[/quote]
你这个写法,onclick里执行都会出错,不应该都是单引号。我不知道你的实际代码情况,按你的意思,我用下面代码试了下,没什么特别的问题。
[code="html"]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">



Untitled function splitHTML(){ var html = "<b>abc<i><u>defg</u></i><u>123</u>456</b>"; var fragment = document.createDocumentFragment(); fragment.innerHTML = html; window.nodes = fragment.childNodes; } window.onload = function(){ document.getElementById('frameShow1').onclick=function(){alert("hahaha");} document.getElementById('frameShow2').onclick=function(){alert("hahaha,two");} }


<div id="frameDiv ">
    <div id="frameShow1" style="width:200px;height:80px;background:#abc;" onclick='alert('1');'></div>
    <div id="frameShow2" style="width:200px;height:80px;background:#def;" onclick='alert('2');'></div> 
</div>



[/code]

[code="html"]

[/code]
代码是复制你的,除了style是我写的。按你说的,我动态绑定的onclick改成这样:
[code="javascript"]
window.onload = function(){
document.getElementById('frameShow1').onclick=function(){alert("hahaha");}
document.getElementById('frameShow2').onclick=function(){alert("hahaha,two");
alert(document.getElementById('frameDiv').innerHTML);
}
}
[/code]
后面alert出来的innerHTML是包含onclick内容的啊,就是
[quote]
onclick='alert('2');'
[/quote]
这个。

用addEventListener或者attachEvent

[quote]
frameDiv.innerHTML打印出来,就没有了onclick属性了,这是我遇到的问题了
[/quote]
你把你这个alert出来的innerHTML截个图给我看看。