在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ JS代碼問(wèn)題,下列代碼求解

JS代碼問(wèn)題,下列代碼求解

<!DOCTYPE html>
<html>
 <head>
  <title> new document </title>  
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   
  <script type="text/javascript">  
     window.onload = function(){  //網(wǎng)頁(yè)加載事件
        Highlight();
     }  
     
     function Highlight(){
         var tbody = document.getElementById('table').lastChild;    //table節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)不是最后一個(gè)tr節(jié)點(diǎn)嘛?怎么成數(shù)組了?
        trs = tbody.getElementsByTagName('tr');   
        for(var i =1;i<trs.length;i++){    
            trs[i].onmouseover = function(){
                this.style.backgroundColor ="#f2f2f2";
            } 
            trs[i].onmouseout = function(){
                this.style.backgroundColor ="#fff";
            } 
        }  
     }
          
     function addOne(obj){ 
        var tbody = document.getElementById('table').lastChild;  
        var tr = document.createElement('tr');  
         
         var td = document.createElement("td");
         td.innerHTML = "<input type='text'/>";
         tr.appendChild(td);
         
         td = document.createElement("td");     
         td.innerHTML = "<input type='text'/>";
         tr.appendChild(td);
         
         td = document.createElement("td");    
         td.innerHTML = "<a href='javascript:;' onclick='deleteRow(this)'>刪除</a>";
         tr.appendChild(td);   
         
         tbody.appendChild(tr);   //table節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)不是最后一個(gè)tr節(jié)點(diǎn)嘛?再添加tr?
        Highlight();
        }

     function deleteRow(obj){
        var tbody = document.getElementById('table').lastChild;   //table節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)不是最后一個(gè)tr節(jié)點(diǎn)嘛?對(duì)它做移除tr怎么個(gè)意思?
        var tr = obj.parentNode.parentNode;
         tbody.removeChild(tr);
     }


  </script> 
 </head> 
 <body> 
       <table border="1" width="50%" id="table">
       <tr>
        <th>學(xué)號(hào)</th>
        <th>姓名</th>
        <th>操作</th>
       </tr>  

       <tr>
        <td>xh001</td>
        <td>王小明</td>
        <td><a href="javascript:;" onclick="deleteRow(this)">刪除</a></td>
       </tr>

       <tr>
        <td>xh002</td>
        <td>劉小芳</td>
        <td><a href="javascript:;" onclick="deleteRow(this)">刪除</a></td>
       </tr>  

       </table>
       <input type="button" value="添加一行" onclick="addOne()" />
 </body>
</html>

var tbody = document.getElementById('table').lastChild; 這里id為table的table節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)不是最后一個(gè)tr節(jié)點(diǎn)嘛?不是只有一個(gè)嘛?怎么成數(shù)組了?

回答
編輯回答
喵小咪

<tbody>標(biāo)簽了解一下

http://www.w3school.com.cn/ta...

MDN的中文翻譯還沒(méi)弄完,不過(guò)只是了解的話w3school夠了。

https://developer.mozilla.org...

2017年6月20日 14:27
編輯回答
墨小羽

Hello,返回的不是數(shù)組節(jié)點(diǎn)。而是table下的tbody節(jié)點(diǎn)標(biāo)簽。
現(xiàn)代瀏覽器為適應(yīng)w3c標(biāo)準(zhǔn)自行添加了這個(gè)標(biāo)簽。
圖片描述

2017年11月30日 20:11