问答详情
源自:9-22 编程练习

新增的行也可以变颜色了~

新增的行也可以变颜色了~ 

提问者:teddybearscm 2015-04-23 15:09

个回答

  • qazyuan
    2016-01-26 15:29:43


    <!DOCTYPE html>

    <html>

     <head>

      <title> new document </title>  

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   

      <script type="text/javascript">  

     

    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);   

     

    document.getElementById('table').lastChild.appendChild(tr);   

    Highlight();

        }


    function deleteRow(obj){     

    var tr = obj.parentNode.parentNode;

    tr.parentNode.removeChild(tr);

    }

    function Highlight(obj){

    var tr=document.getElementsByTagName('tr');

    for(var i=0;i<tr.length;i++){

    tr[i].onmouseover=function(){

    this.style.backgroundColor="#bbb";

    };

    tr[i].onmouseout=function(){

    this.style.backgroundColor="#fff";

    }

    }

    }


    // var tr=document.getElementById('table').childNodes;

      </script> 

     </head> 

     <body> 

      <table border="1" width="50%" id="table">

      <tr>

    <th>编号</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>