动态添加的SELECT元素不会在Internet Explorer中触发onchange事件

Age*_*191 9 html javascript internet-explorer

据我所知,这只是在Internet Explorer中被破坏了.我有一个脚本,可以创建多个动态<select>元素并为它们添加onchange事件.onchange事件在Firefox中触发没有问题,但在Internet Explorer中它永远不会触发.使用开发人员工具栏我看到DOM列出了正确的事件,它永远不会触发.我把问题归结为以下代码:

<html>
    <head>
        <script language="javascript">
            function addSelect() {
                var se = document.createElement('select');
                se.setAttribute("onchange", "alert('Dynamic')");
                se.options[0] = new Option("1", "1");
                se.options[1] = new Option("2", "2");
                se.options[2] = new Option("3", "3");
                se.options[3] = new Option("4", "4");
                var plh = document.getElementById("ph");
                plh.appendChild(se);
            }
        </script>
    </head>
    <body onload="addSelect()">
        <select name="something" onchange="alert('Static')">
            <optgroup label="set1">
            <option value="1">1</option>
            <option value="2">2</option>
            </optgroup>
            <optgroup label="set2">
            <option value="3">3</option>
            <option value="4">4</option>
            </optgroup>
        </select>
        <div id="ph">
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

静态警报消息很好,但动态消息在Internet Explorer中不起作用.我几乎肯定我在其他地方见过这项工作,但我似乎无法找到其他例子.有没有人看到/知道如何使这个工作?

Cre*_*esh 11

更改:

se.setAttribute("onchange", "alert('Dynamic')");
Run Code Online (Sandbox Code Playgroud)

至:

se.setAttribute("onchange", function(){alert('Dynamic')});
Run Code Online (Sandbox Code Playgroud)

甚至更短:

se.onchange = function(){alert('Dynamic')};
Run Code Online (Sandbox Code Playgroud)