在table中插入多行,能使用与insertAdjacentHTML相似的功能

<html>
<body>
<table id=tableList border=1 width=100>
    <tr><td>1</td></tr>
</table>
<button onclick=addRow()>插入行</button>
</body>

<script type="text/javascript">
var num = 1;
function addRow()
{
 num ++;
 InsertRow(tableList,"<tr><td>"+num+"新行</td></tr>");
}
function InsertRow(table,rowHtml)
{
    var o=document.createElement("div"),ol;
    o.innerHTML="<table>"+rowHtml+"</table>"
    ol=o.childNodes[0].tBodies[0].rows
    while(ol.length>0){
        table.tBodies[0].appendChild(ol[0])
    }
}
</script>

Table的 tBodies属性是一个JS中的集合,而不是数组,没有sort()方法,所以不能用来直接排序。

原文地址:https://www.cnblogs.com/OwenWu/p/1762934.html