<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方案在此处不可行

解决方案 »

  1.   


    select1.onchange=hello;不要用
     select1.setAttribute("onchange","hello();");
      

  2.   

    要注意的是select1.onchange=hello;的hello后没有括号
    select1.onchange=function(){hello();};这样写也可以
      

  3.   

    select1.attachEvent("onchange",function(){hello();});
      

  4.   

    谢谢各位,但是我的实际应用中的hello()函数中是要传一个参数的,就是要把自己这个select对象传过去,我的方法是hello(this); 如果写成1楼的说法,我参数如何传呢?
      

  5.   

    select1.onchange=function(){hello(select1);};
      

  6.   

    select1.onchange=function(){hello(select1);};