JavaScript 事件在 IE 8 中不起作用

标签 javascript internet-explorer internet-explorer-8

IE8 好像忽略了我的点击事件!当我按“+”按钮时,它应该添加一个新行,并在“-”上删除它。我是 WEB 开发的新手,但我看不到任何其他问题。我测试了一下,在IE10、IE9下工作正常,但在IE8上不行!你能帮我解决这个问题吗!

我的 HTML 代码:

<table id="sysAffectedTable2">
    <thead>
        <tr>
            <th>#</th>
            <th><span class="locationName">Location Name</span></th>
            <th>Subnet (Optional)</th>
            <th>Add</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td><input type="text" id="locationName1" value="" name="locationName1"/></td>
            <td><input type="text" id="subnet1" value="" name="subnet1"/></td>
            <td><input type="button" style="width: 40px" id="addDiskButton2" value="  +  " onclick="insertRow2()"/></td>
            <td><input type="button" style="width: 40px" id="delDiskButton2" value="  -  " onclick="deleteRow2(this)"/></td>    
        </tr>
    </tbody>        
    </table>    

我的 JavaScript 代码:

<script type="text/javascript" language="javascript">
    var maxRow2=1;


    var table2 = document.getElementById('sysAffectedTable2'), tbody2 = table2
            .getElementsByTagName('tbody')[0], clone2 = tbody2.rows[0]
            .cloneNode(true);

    function deleteRow2(el) {
        var i = el.parentNode.parentNode.rowIndex;

        if (i != 1) {
            table2.deleteRow(i);
            while (table2.rows[i]) {
                updateRow2(table2.rows[i], i, false);
                i++;
            }
            maxRow2--;
        } else if (i == 1) {
            table2.deleteRow(i + 1);
            while (table2.rows[i + 1]) {
                updateRow2(table2.rows[i + 1], i + 1, false);
                i++;
            }
            maxRow2--;
        }
    }

    function insertRow2() {
        if (maxRow2 < 32) {
            var new_row = updateRow2(clone2.cloneNode(true),
                    ++tbody2.rows.length, true);
            tbody2.appendChild(new_row);
            maxRow2++;
        }
    }

    function updateRow2(row2, a2, reset2) {
        row2.cells[0].innerHTML = a2;

        var inp1 = row2.cells[1].getElementsByTagName('input')[0];
        var inp2 = row2.cells[2].getElementsByTagName('input')[0];

        inp1.name = 'locationName' + a2;
        inp1.id = 'locationName' + a2;
        inp2.name = 'subnet' + a2;
        inp2.id = 'subnet' + a2;

        if (reset2) {
            inp1.value = inp2.value = '';
        }
        return row2;
    }
</script>

您可以在直播中看到它 here -http://jsfiddle.net/yHANj/1/

请你帮帮我!

提前致谢!

最佳答案

此行遇到错误:++tbody2.rows.length

显然 IE8 的 javascript 引擎对此的处理方式与其他浏览器不同,但是能够更改any<中表格行数的 length 属性是没有意义的。/em> 浏览器。如果您想更改表中的行数,可以通过插入新的 DOM 元素(通过 .appendChild() 调用来实现)来实现。

将其更改为 tbody2.rows.length + 1,而不是尝试直接增加 length 属性。

编辑:确实,如果您尝试增加其他浏览器的长度,该语句似乎会被忽略。 IE8 抛出异常而不是忽略它。

关于JavaScript 事件在 IE 8 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19229605/

相关文章:

javascript - jQuery 函数使用 href 而不是 input?

javascript - 使用 Coldfusion 将特定 url 插入到 javascript 函数

internet-explorer - 没有 iframe 的 IE 8 和 9 中的跨域 cookie?

jquery - (JQuery) IE 中的圆 Angular ?

performance - 如何禁用 IE8 脚本错误信息?

javascript - IE8 对于最新的 jquery 没有圆 Angular

javascript - 从 ng-option 中的对象保存特定字段?并设置第一个默认值

javascript - 在 JavaScript 或 jQuery 中,如何将 ontouchmove 事件转换为目标上的基本单击事件

IE 中的 ASP.NET 页面在其他程序中窃取焦点

javascript - Traceur 在多大程度上编译为 IE8 兼容的 Javascript?