
<html>
<head>
<title>JS测试</title>
<script type="text/javascript">
var num = 2;
function addRow(){
var tr = document.createElement("tr");
var td = document.createElement("td");
var te = document.createTextNode('row '+num++);
td.appendChild(te);
tr.appendChild(td);
var table = document.getElementById("table");
if(tbody.rows.length == 6){

alert('不能加了!');
}else{
table.appendChild(tr);
}
}
</script>
</head>
<body">
<table>
<tr>
<th>行数</th>
</tr>
<tr>

<td>row 1</td>
</tr>
</table>
<input type="button" value="Button">
</body>
</html>
上面JS代码不能向<table>中添加行ie浏览器中appendie浏览器中append,因为IE中<table>标签不支持appendChild()方法.
解决办法:在<table>标签中加入<tbody>标签,使用tbody元素加入新行.
<html>
<head>
<title>JS测试</title>
<script type="text/javascript">
var num = 2;

function addRow(){
var tr = document.createElement("tr");
var td = document.createElement("td");
var te = document.createTextNode('row '+num++);
td.appendChild(te);
tr.appendChild(td);
var tbody= document.getElementById("tbody");
if(tbody.rows.length == 6){
alert('不能加了!');
}else{
tbody.appendChild(tr);
}
}

</script>
</head>
<body">
<table>
<tbody>
<tr>
<th>行数</th>
</tr>
<tr>
<td>row 1</td>
</tr>
</tbody>
</table>
<input type="button" value="Button">
</body>
</html>
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-132945-1.html
米国出了就没有放弃对中国在政治上