html中動態建立表格

來源:互聯網
上載者:User

標籤:function   圖片   動態   

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>dtTable.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="this is my page">

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

<script type="text/javascript">

//dom結構繪製完畢,頁面的所有關聯的檔案必須載入完畢(圖片)

window.onload=function(){

document.getElementById("addUser").onclick=function(){

/*************************************************************************/

//擷取文字框中的值

var nameValue=document.getElementById("name").value;

var emailValue=document.getElementById("email").value;

var telValue=document.getElementById("tel").value;

/*************************************************************************/

//建立nametd

var nameTd=document.createElement("td");

var nameText=document.createTextNode(nameValue);

nameTd.appendChild(nameText);

//建立emailtd

var emailTd=document.createElement("td");

var emailText=document.createTextNode(emailValue);

emailTd.appendChild(emailText);

//建立teltd

var telTd=document.createElement("td");

var telText=document.createTextNode(telValue);

telTd.appendChild(telText);

//建立atd

var aTd=document.createElement("td");//元素節點

var aElement=document.createElement("a");

aElement.setAttribute("href","index.html?id="+nameValue );

var deleteText=document.createTextNode("Delete");//內容節點

aElement.appendChild(deleteText)

aTd.appendChild(aElement);

/*************************************************************************/

//建立tr

var trElement=document.createElement("tr");

//增加td到tr上

trElement.appendChild(nameTd);

trElement.appendChild(emailTd);

trElement.appendChild(telTd);

trElement.appendChild(aTd);

/*************************************************************************/

//增加tr到tbody上

var tableElement=document.getElementById("usertable");

//建立tbody,為了跨瀏覽器

var tbodyElement=document.createElement("tbody");

tbodyElement.appendChild(trElement);

tableElement.appendChild(tbodyElement);

/*************************************************************************/

//刪除

aElement.onclick=function(){

//使網頁的連結失效

return delTr(aElement);

}

/*************************************************************************/

}

}

function delTr(aElement){

var name=aElement.parentNode.parentNode.firstChild.firstChild.nodeValue;

//alert(name)

var flag=window.confirm("您真的要刪除"+name+"這個使用者嗎?");

if(!flag){

return false;

}

//擷取tbody

var tbodyElement=aElement.parentNode.parentNode.parentNode;

//擷取tr

var trElement=aElement.parentNode.parentNode;

//刪除

tbodyElement.removeChild(trElement);

//是網頁連結失效

return false;

}

</script>

  </head>

  

  <body>

      <center>

        <br><br>

                      添加使用者:<br><br>

                      姓名:<input type="text" name="name" id="name"/>&nbsp;&nbsp;

        email:<input type="text" name="email" id="email"/>&nbsp;&nbsp;

                      電話:<input type="text" name="tel" id="tel"/><br/><br/>

      <button id="addUser">提交</button>

      <br/><br/>

      <hr/>

      <br/><br/>

      <table id="usertable" border="1" cellpadding="5" cellspacing="0">

      <tr>

      <th>姓名</th>

      <th>email</th>

      <th>電話</th>

      <th>操作</th>

      </tr>

      <tr>

      <td>Tom</td>

      <td>[email protected]</td>

      <td>100000</td>

      <td><a href="deleteEmp?id=Tom">Delete</a></td>

      </tr>

      </table>

      

      </center>

  </body>

</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.