<html>
<head>
<title>a.html</title>
<script type="text/javascript">
function hello() {
var div = this.document.getElementById("div");
var select1 = this.document.createElement("select");
var option11 = this.document.createElement("option");
option11.setAttribute("id","option01");
var text = this.document.createTextNode("aaaaaaaaaaa");
option11.appendChild(text);
var option12 = this.document.createElement("option");
option12.setAttribute("id","option02");
var text = this.document.createTextNode("bbbbbbbbbbb");
option12.appendChild(text);
select1.appendChild(option11);
select1.appendChild(option12);
select1.setAttribute("onchange","hello();");
div.appendChild(select1);
}
</script>
</head>
<body>
<div id="div">
<select onchange="hello();">
<option value="a">aaaa</option>
<option value="b">bbbb</option>
</select>
</div>
</body>
</html>这个例子很简单,想实现的效果就是当一个下拉列表的值改变的时候从新在后面用hello()函数生成一个select,
生成的新的select也会有onchange属性,如果改变也想去触发hello函数,但是现在的问题,用JS生成的select
在FF下可以正常触发onchange事件,但是在IE下面却怎么也不触发,想请各位高手帮助分析分析,有什么解决方案?
PS: innerHtml方案在此处不可行
select1.onchange=hello;不要用
select1.setAttribute("onchange","hello();");
select1.onchange=function(){hello();};这样写也可以