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/