JavaScript操作DOM(動態表格處理)

來源:互聯網
上載者:User

標籤:head   body   orm   create   增加   ddb   log   onload   text   

<html><title>動態處理表格式資料</title>    <head>    <script type="text/javascript">          function clearForm(){              document.getElementById("deptno").value="";              document.getElementById("dname").value="";          }          window.onload=function(){             document.getElementById("addBtn").addEventListener("click",onloadData,false);          }          function onloadData(){          var deptno=document.getElementById("deptno").value;          var dname=document.getElementById("dname").value;          addRow(deptno,dname);          clearForm();          }          //刪除:找到父元素,調用其removeChild("指定刪除的元素")          function removeRow(deptno){          var deptRow=document.getElementById("dept-"+deptno);          var dept=document.getElementById("dept-body");          dept.removeChild(deptRow);          }          function addRow(deptno,dname){          var trElt=document.createElement("tr");          trElt.setAttribute("id","dept-"+deptno);//設定tr的屬性          var deptnoTd=document.createElement("td");          var dnameTd=document.createElement("td");          var delTd=document.createElement("td");                             var delBtn=document.createElement("button");//建立button按鈕                 delBtn.appendChild(document.createTextNode("刪除"));//追加文本資料                 delBtn.addEventListener("click",function(){//添加事件               removeRow(deptno);                 },false);                            delTd.appendChild(delBtn);          deptnoTd.appendChild(document.createTextNode(deptno));          dnameTd.appendChild(document.createTextNode(dname));          trElt.appendChild(deptnoTd);          trElt.appendChild(dnameTd);          trElt.appendChild(delTd);          document.getElementById("dept-body").appendChild(trElt);          }    </script>    </head>    <body>        <div>        部門編號:<input type="text" name="deptno" id="deptno"/><br>        部門名稱:<input type="text" name="dname" id="dname"/><br>            <input type="button" value="增加" id="addBtn"/>             <input type="button" value="清空" onclick="clearForm()"/>        </div>        <div>    <table border="1"  width="80%">    <thead >    <tr>      <td>部門編號</td>      <td>部門名稱</td>      <td>操作</td>                 </tr>    </thead>    <tbody id="dept-body">        </tbody>    </table>    </div>    </body></html>

  

JavaScript操作DOM(動態表格處理)

聯繫我們

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