js動(dòng)態(tài)控制table的tr、td增加及刪除的具體實(shí)現(xiàn)
更新時(shí)間:2014年04月30日 11:20:27 作者:
這篇文章主要介紹了使用js如何動(dòng)態(tài)控制table的tr,td增加及刪除,需要的朋友可以參考下
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>序號(hào)</th>
<th>機(jī)器名</th>
<th>IP地址</th>
<th>MAC地址</th>
<th>上行/下行速率</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
js:
增加:
if(條件)
{//根據(jù)InterfaceType的值區(qū)分無線客戶端和有限客戶端
var table = document.getElementById("wifi_clients_table");
var newRow = table.insertRow(); //創(chuàng)建新行
var newCell1 = newRow.insertCell(0); //創(chuàng)建新單元格
newCell1.innerHTML = "<td>1</td>" ; //單元格內(nèi)的內(nèi)容
newCell1.setAttribute("align","center"); //設(shè)置位置
var newCell2 = newRow.insertCell(1); //創(chuàng)建新單元格
newCell2.innerHTML = "<td>"+info.LANHosts.HostName+"</td>";
newCell2.setAttribute("align","center"); //設(shè)置位置
var newCell3 = newRow.insertCell(2); //創(chuàng)建新單元格
newCell3.innerHTML = "<td>"+info.LANHosts.IPAddress+"</td>";
newCell3.setAttribute("align","center"); //設(shè)置位置
var newCell4 = newRow.insertCell(3); //創(chuàng)建新單元格
newCell4.innerHTML = "<td>"+info.LANHosts.MACAddress+"</td>";
newCell4.setAttribute("align","center"); //設(shè)置位置
var newCell5 = newRow.insertCell(4); //創(chuàng)建新單元格
newCell5.innerHTML = "<td>"+info.LANHosts.UpRate+"/"+info.LANHosts.DownRate+"kb</td>";
newCell5.setAttribute("align","center"); //設(shè)置位置
}
刪除:在頁面關(guān)閉時(shí)清除,下次訪問時(shí)再重新生成,防止每次tr遞增,頁面錯(cuò)亂
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;
}
}
復(fù)制代碼 代碼如下:
<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>序號(hào)</th>
<th>機(jī)器名</th>
<th>IP地址</th>
<th>MAC地址</th>
<th>上行/下行速率</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
js:
增加:
復(fù)制代碼 代碼如下:
if(條件)
{//根據(jù)InterfaceType的值區(qū)分無線客戶端和有限客戶端
var table = document.getElementById("wifi_clients_table");
var newRow = table.insertRow(); //創(chuàng)建新行
var newCell1 = newRow.insertCell(0); //創(chuàng)建新單元格
newCell1.innerHTML = "<td>1</td>" ; //單元格內(nèi)的內(nèi)容
newCell1.setAttribute("align","center"); //設(shè)置位置
var newCell2 = newRow.insertCell(1); //創(chuàng)建新單元格
newCell2.innerHTML = "<td>"+info.LANHosts.HostName+"</td>";
newCell2.setAttribute("align","center"); //設(shè)置位置
var newCell3 = newRow.insertCell(2); //創(chuàng)建新單元格
newCell3.innerHTML = "<td>"+info.LANHosts.IPAddress+"</td>";
newCell3.setAttribute("align","center"); //設(shè)置位置
var newCell4 = newRow.insertCell(3); //創(chuàng)建新單元格
newCell4.innerHTML = "<td>"+info.LANHosts.MACAddress+"</td>";
newCell4.setAttribute("align","center"); //設(shè)置位置
var newCell5 = newRow.insertCell(4); //創(chuàng)建新單元格
newCell5.innerHTML = "<td>"+info.LANHosts.UpRate+"/"+info.LANHosts.DownRate+"kb</td>";
newCell5.setAttribute("align","center"); //設(shè)置位置
}
刪除:在頁面關(guān)閉時(shí)清除,下次訪問時(shí)再重新生成,防止每次tr遞增,頁面錯(cuò)亂
復(fù)制代碼 代碼如下:
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;
}
}
相關(guān)文章
javascript instanceof,typeof的區(qū)別
區(qū)分string 與 String的區(qū)別2010-03-03
js數(shù)組實(shí)現(xiàn)權(quán)重概率分配
今天寫了一個(gè)js控制頁面輪播的功能,如果僅僅使用隊(duì)列很簡(jiǎn)單,但是考慮到為每一個(gè)頁面分配權(quán)重的是否變的異常復(fù)雜,使用switch和if else也無法解決,于是想到使用js數(shù)組實(shí)現(xiàn)2017-09-09
js中AppendChild與insertBefore的用法詳細(xì)解析
這篇文章主要是對(duì)js中AppendChild與insertBefore的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12
手把手教你uniapp和uview2.0實(shí)現(xiàn)表單校驗(yàn)實(shí)戰(zhàn)
表單提交對(duì)大家來說應(yīng)該都不陌生,這是個(gè)很常見的功能,這篇文章主要給大家介紹了關(guān)于手把手教你uniapp和uview2.0實(shí)現(xiàn)表單校驗(yàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
深入分析javascript中的錯(cuò)誤處理機(jī)制
這篇文章主要介紹了深入分析javascript中的錯(cuò)誤處理機(jī)制的相關(guān)資料,需要的朋友可以參考下2016-07-07
JavaScript實(shí)現(xiàn)的圓形浮動(dòng)標(biāo)簽云效果實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的圓形浮動(dòng)標(biāo)簽云效果,涉及javascript字符串與數(shù)組的遍歷、排序操作以及元素樣式動(dòng)態(tài)操作與數(shù)學(xué)運(yùn)算等相關(guān)技巧,是非常實(shí)用的一段代碼,需要的朋友可以參考下2015-08-08
JavaScript實(shí)現(xiàn)手機(jī)號(hào)碼 3-4-4格式并控制新增和刪除時(shí)光標(biāo)的位置
這篇文章主要介紹了JavaScript實(shí)現(xiàn)手機(jī)號(hào)碼 3-4-4格式并控制新增和刪除時(shí)光標(biāo)的位置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

