我们可以删除dom表吗
can we remove dom table?
根据用户的选择,我必须在两个表之间切换。我可以使用表对象模型进行切换,但它有问题,因为我删除了一个表并加载了另一个表,它可以工作,但下次切换回第一个表时,它将无法获得表ID,也无法工作。
在使用像这样的DOM时
var tbl1 = document.createElement("table");
tbl1.setAttribute('id','shorttable');
对于删除,我使用以下java脚本
var b = document.getElementById('shorttable');
document.removeChild(b);
但给了我例外
未捕获错误:NOT_FOUND_ERR:DOM异常
使用DOM有什么方法可以做到这一点吗?
@孤独的一天,谢谢你的回复,但当我从其他功能调用它时,它不起作用?我尝试的是这样的。
<html>
<head>
<title>test dom</title>
<script>
function trackmode()
{
// i have to delete other table from this function where it is not working
var b = document.getElementById('shorttable');
alert(b);
b.parentNode.removeChild(b);
var body = document.getElementsByTagName("body")[0];
// creates a <table> element and a <tbody> element
var tbl = document.createElement("table");
tbl.setAttribute('id','tracktable');
var tblBody = document.createElement("tbody");
// creating all cells
for (var j = 0; j < 2; j++) {
// creates a table row
var row = document.createElement("tr");
for (var i = 0; i < 2; i++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the table row
var cell = document.createElement("td");
var cellText = document.createTextNode("cell is row "+j+", column "+i);
cell.appendChild(cellText);
row.appendChild(cell);
}
// add the row to the end of the table body
tblBody.appendChild(row);
}
// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// appends <table> into <body>
body.appendChild(tbl);
// sets the border attribute of tbl to 2;
tbl.setAttribute("border", "2");
}
function shortenmode()
{
var b = document.getElementById('tracktable');
b.parentNode.removeChild(b);
var body1 = document.getElementsByTagName("body")[0];
// creates a <table> element and a <tbody> element
var tbl1 = document.createElement("table");
tbl1.setAttribute('id','shorttable');
var tblBody1 = document.createElement("tbody");
// creating all cells
for (var j = 0; j < 2; j++) {
// creates a table row
var row = document.createElement("tr");
for (var i = 0; i < 2; i++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the table row
var cell = document.createElement("td");
var cellText = document.createTextNode("cell is row "+j+", column "+i);
cell.appendChild(cellText);
row.appendChild(cell);
}
// add the row to the end of the table body
tblBody1.appendChild(row);
}
// put the <tbody> in the <table>
tbl1.appendChild(tblBody1);
// appends <table> into <body>
body1.appendChild(tbl1);
// sets the border attribute of tbl to 2;
tbl1.setAttribute("border", "2");
//here it will work
//var b = document.getElementById('shorttable');
//alert(b);
//b.parentNode.removeChild(b);
//myP.removeChild(myTextNode);
}
</script>
</head>
<body >
<button type="button" name="website" onclick=trackmode() > track</button>
<button type="button" name="website1" onclick=shortenmode() > short </button>
</body>
</html>
要删除元素,需要将其从父级中删除,而不是从文档中删除。因此:
b.parentNode.removeChild(b);
相关文章:
- 你能在JS中获得一个样式而不将其附加到DOM元素吗
- 我应该只在指令的链接 fn 中进行 DOM 操作吗?
- 你能用detectmobilebrowser链接样式表吗
- 我必须在sqlitedb的第一次填充时创建所有表吗
- 用硒填充和操作jqGrid的搜索小部件似乎不会触发DOM表单事件
- 解析自定义网络钩子:我可以查询我的表吗?
- 在数据表表和普通 DOM 表之间移动(添加/删除)tr 行
- jQuery remove 函数真的删除了 Dom 元素吗?
- 我们可以在不将其加载到无限可滚动网页中的情况下访问 DOM 元素吗?
- Javascript可以修改HTML流而不是DOM树吗?
- 我们可以使用 postMessage 传递 DOM 元素吗?
- 这将使用Javascript生成一个动态HTML表吗
- 我可以忽略其他dom元素中的html dom元素吗
- 我可以在相位器游戏窗口上放置Dom元素吗
- 我应该通过id/类名或数据属性来选择DOM元素吗
- 我可以使用Knockout将绑定应用到多个DOM元素吗?
- DOM表单值操作
- 使用DOM表单名中的变量
- 我可以基于到不同数据对象的数据联接来更新D3选择器中的DOM元素吗
- 我们可以删除dom表吗