添加元素節點-html

來源:互聯網
上載者:User

標籤:

<!DOCTYPE html><html><head><title>節點添加與刪除練習</title><meta http-equiv="content-type" content="text/html; charset=UTF-8"><style type="text/css">div {    text-align: center;    background-color: #c0c0c0;    width: 65%;    height: 65%;}</style><script type="text/javascript">    //要求:動態向table中添加資訊,並可以刪除    window.onload = function() {        /*         當點擊按鈕時,將資訊得到,並手動封裝成字串         "<tr><td>username</td><td>email</td><td>telephone</td><td><a href=‘#‘ onclick=‘delRow(this)‘>Delete</a></td></tr>"                 得到id為tab的table,合適innerHTML+=上面的字串.                         問題:當點擊超連結時,怎樣刪除當前行.                 當超連結被點擊時,調用一個delRow方法,將this傳遞到delRow方法中.         在delRow方法中可以通過 parentNode.parentNode得到最終要刪除的tr。         在通過table調用removeChild方法將這一行刪除。         */                  document.getElementById("btn").onclick=function(){             //1.得到資訊.             var username=document.getElementById("username").value;             var email=document.getElementById("email").value;             var telephone=document.getElementById("telephone").value;                          //2.拼出字串.             var msg="<tr><td>"+username+"</td><td>"+email+"</td><td>"+telephone+"</td><td><a href=‘#‘ onclick=‘delRow(this)‘>Delete</a></td></tr>"                         //3.將msg添加到table中.                        document.getElementById("tab").innerHTML+=msg;                          document.getElementById("username").value="";             document.getElementById("email").value="";             document.getElementById("telephone").value="";         }    };        function delRow(t){        document.getElementById("tab").removeChild(t.parentNode.parentNode.parentNode);                //t.parentNode.parentNode.parentNode.removeChild(t.parentNode.parentNode);    }</script></head><body>    <div>        <br> <br> 姓名: <input type="text" id="username">        EMAIL: <input type="text" id="email"> 電話: <input type="text"            id="telephone"> <br> <br> <input type="button"            value="添加" id="btn">        <hr>        <table id="tab" border=‘1‘ align="center" width="85%">            <tr>                <th>姓名</th>                <th>EMAIL</th>                <th>電話</th>                <th>操作</th>            </tr>        </table>    </div></body></html>

 

添加元素節點-html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.