js動態控制table的tr、td增加及刪除的具體實現_javascript技巧

來源:互聯網
上載者:User
html:
複製代碼 代碼如下:

<table id='wifi_clients_table' style="color:#0099CC;font-size:12px;" class="table table-striped table-bordered table-hover table-condensed">
<thead>
<tr class="success">
<th>序號</th>
<th>機器名</th>
<th>IP地址</th>
<th>MAC地址</th>
<th>上行/下行速率</th>
</tr>
</thead>
<tbody>

</tbody>
</table>

js:

增加:
複製代碼 代碼如下:

if(條件)
{//根據InterfaceType的值區分無線用戶端和有限用戶端
var table = document.getElementById("wifi_clients_table");
var newRow = table.insertRow(); //建立新行

var newCell1 = newRow.insertCell(0); //建立新儲存格
newCell1.innerHTML = "<td>1</td>" ; //儲存格內的內容
newCell1.setAttribute("align","center"); //設定位置

var newCell2 = newRow.insertCell(1); //建立新儲存格
newCell2.innerHTML = "<td>"+info.LANHosts.HostName+"</td>";
newCell2.setAttribute("align","center"); //設定位置

var newCell3 = newRow.insertCell(2); //建立新儲存格
newCell3.innerHTML = "<td>"+info.LANHosts.IPAddress+"</td>";
newCell3.setAttribute("align","center"); //設定位置

var newCell4 = newRow.insertCell(3); //建立新儲存格
newCell4.innerHTML = "<td>"+info.LANHosts.MACAddress+"</td>";
newCell4.setAttribute("align","center"); //設定位置

var newCell5 = newRow.insertCell(4); //建立新儲存格
newCell5.innerHTML = "<td>"+info.LANHosts.UpRate+"/"+info.LANHosts.DownRate+"kb</td>";
newCell5.setAttribute("align","center"); //設定位置

}

刪除:在頁面關閉時清除,下次訪問時再重建,防止每次tr遞增,頁面錯亂
複製代碼 代碼如下:

var t1=document.getElementById("lan_clients_table");

var rowNum=t1.rows.length;
if(rowNum>0)
{
for(i=0;i<rowNum;i++)
{
t1.deleteRow(i);
rowNum=rowNum-1;
i=i-1;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.