似乎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引擎与其他浏览器对待此问题的方式不同,但是在任何浏览器中都无法更改表的行数的length属性没有任何意义。如果要更改表中的行数,可以通过插入一个新的DOM元素来实现(通过.appendChild()调用即可完成)。

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

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

09-12 07:45
查看更多