JavaScript:动态生成表格 | 您所在的位置:网站首页 › 动态生成表格html › JavaScript:动态生成表格 |
如果要想动态操作表格,那么有一个前提:强烈建议要定义好表格,而后只操作,元素即可 TestDemo1.htm var count=0;window.onload=function(){document.getElementById("addBut").addEventListener("click",insertTable);}function insertTable(){var tr=document.createElement("tr");var deptnotd=document.createElement("td");var dnameTd=document.createElement("td");var operTd=document.createElement("td");deptnotd.appendChild(document.createTextNode(count));dnameTd.appendChild(document.createTextNode("部门名称-"+count));var deleteButton=document.createElement("input");deleteButton.setAttribute("type","button");deleteButton.setAttribute("value","删除部门");deleteButton.addEventListener("click",function(){tr.parentNode.removeChild(tr);},false)operTd.appendChild(deleteButton);count ++;tr.appendChild(deptnotd);tr.appendChild(dnameTd);tr.appendChild(operTd);document.getElementById("tab").appendChild(tr);} 部门编号部门名称操作如果在非IE浏览器的情况下,只需要在元素里面设置元素,而后在元素下设置元素,在IE下,需要将设置到元素中,再将元素设置再元素下即可。 利用JavaScript+DOM操作对于页面的动态效果实在是太任意实现了。 |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |