动态删除 DOM 表中的行
我在下面的循环中遇到了removeChild函数的问题,并且无法真正找到好的例子?我的代码有什么问题吗?
var univArray = new Array(
{name: "Stanford University", nick: "Stanford", ownership: "private", sys: "n/a", SATh: 1550, SATl: 1360, tuition: 27204, room: 8680},
{name: "University of California, Berkeley", nick: "UC Berkeley", ownership: "public", sys: "University of California", SATh: 1440, SATl: 1170, tuition: 4200, room: 10608},
{name: "University of California, Santa Cruz", nick: "UC Santa Cruz", ownership: "public", sys: "University of California", SATh: 1270, SATl: 1030, tuition: 4384, room: 9708},
{name: "San Francisco State University", nick: "SFSU", ownership: "public", sys: "CalState", SATh: 1120, SATl: 850, tuition: 1826, room: 6736},
{name: "San Jose State University", nick: "SJSU", ownership: "public", sys: "CalState", SATh: 1140, SATl: 860, tuition: 1912, room: 7395},
{name: "Sonoma State University", nick: "Sonoma State", ownership: "public", sys: "CalState", SATh: 1140, SATl: 930, tuition: 2226, room: 9606},
{name: "California State University, Hayward", nick: "CalState Hayward", ownership: "public", sys: "CalState", SATh: 1050, SATl: 810, tuition: 1800, room: 6435},
{name: "University of San Francisco", nick: "USF", ownership: "private", sys: "Roman Catholic", SATh: 1240, SATl: 1030, tuition: 21780, room: 9080},
{name: "Santa Clara University", nick: "SCU", ownership: "private", sys: "Roman Catholic", SATh: 1300, SATl: 1110, tuition: 23445, room: 8904},
{name: "Mills College", nick: "Mills College", ownership: "private", sys: "n/a", SATh: 1130, SATl: 920, tuition: 19482, room: 7832}
);
var table = document.createElement("table")
document.body.appendChild(table)
table.setAttribute("id", "tableid")
var tbody = document.createElement("tbody")
table.appendChild(tbody)
var row = document.createElement("tr")
tbody.appendChild(row)
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Name"))
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Sat High"))
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Sat Low"))
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Tuition"))
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Room and Board"))
for (var i = 0; i < univArray.length; i++) {
var tbody = document.createElement("tbody")
table.appendChild(tbody)
var row = document.createElement("tr")
tbody.appendChild(row)
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].name))
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].SATh))
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].SATl))
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].tuition))
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].room))
}
function filter(){
if (document.getElementById("public").checked) {
var nodes = document.body.getElementsByTagName('tr')
for (var z = 0; z < univArray.length; z++) {
var node = nodes[z];
if (univArray[z].ownership != "public") {
node.parentNode.removeChild(node)
}
}
}
}
document.getElementById("update").onclick = filter;
I am having trouble with the removeChild function in the loop below and couldnt really find good examples for it? what is wrong with my code?
var univArray = new Array(
{name: "Stanford University", nick: "Stanford", ownership: "private", sys: "n/a", SATh: 1550, SATl: 1360, tuition: 27204, room: 8680},
{name: "University of California, Berkeley", nick: "UC Berkeley", ownership: "public", sys: "University of California", SATh: 1440, SATl: 1170, tuition: 4200, room: 10608},
{name: "University of California, Santa Cruz", nick: "UC Santa Cruz", ownership: "public", sys: "University of California", SATh: 1270, SATl: 1030, tuition: 4384, room: 9708},
{name: "San Francisco State University", nick: "SFSU", ownership: "public", sys: "CalState", SATh: 1120, SATl: 850, tuition: 1826, room: 6736},
{name: "San Jose State University", nick: "SJSU", ownership: "public", sys: "CalState", SATh: 1140, SATl: 860, tuition: 1912, room: 7395},
{name: "Sonoma State University", nick: "Sonoma State", ownership: "public", sys: "CalState", SATh: 1140, SATl: 930, tuition: 2226, room: 9606},
{name: "California State University, Hayward", nick: "CalState Hayward", ownership: "public", sys: "CalState", SATh: 1050, SATl: 810, tuition: 1800, room: 6435},
{name: "University of San Francisco", nick: "USF", ownership: "private", sys: "Roman Catholic", SATh: 1240, SATl: 1030, tuition: 21780, room: 9080},
{name: "Santa Clara University", nick: "SCU", ownership: "private", sys: "Roman Catholic", SATh: 1300, SATl: 1110, tuition: 23445, room: 8904},
{name: "Mills College", nick: "Mills College", ownership: "private", sys: "n/a", SATh: 1130, SATl: 920, tuition: 19482, room: 7832}
);
var table = document.createElement("table")
document.body.appendChild(table)
table.setAttribute("id", "tableid")
var tbody = document.createElement("tbody")
table.appendChild(tbody)
var row = document.createElement("tr")
tbody.appendChild(row)
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Name"))
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Sat High"))
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Sat Low"))
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Tuition"))
header =document.createElement("th")
row.appendChild(header)
header.appendChild(document.createTextNode("Room and Board"))
for (var i = 0; i < univArray.length; i++) {
var tbody = document.createElement("tbody")
table.appendChild(tbody)
var row = document.createElement("tr")
tbody.appendChild(row)
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].name))
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].SATh))
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].SATl))
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].tuition))
cell = document.createElement("td")
row.appendChild(cell)
cell.appendChild(document.createTextNode(univArray[i].room))
}
function filter(){
if (document.getElementById("public").checked) {
var nodes = document.body.getElementsByTagName('tr')
for (var z = 0; z < univArray.length; z++) {
var node = nodes[z];
if (univArray[z].ownership != "public") {
node.parentNode.removeChild(node)
}
}
}
}
document.getElementById("update").onclick = filter;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getElementsByTagName
返回一个 NodeList,正如 W3C 所说:“DOM 中的 NodeList 对象是活动的。”。这意味着当您从 DOM 树中删除节点时,它也会从nodes
列表变量中删除。因此,您需要始终删除列表的第一个元素:getElementsByTagName
returns a NodeList and as W3C says: "NodeList objects in the DOM are live.". This means when you remove a node from you DOM tree, it's also removed from yournodes
list variable. So you need always remove the first element of your list:感谢您的帮助。我尝试遵循您的建议,但 if 语句似乎被忽略了。这是我现在所拥有的:
Thanks for your help. I tried following your suggestion, but the if statement seems to be getting ignored. Here's what I have now:
尽管用户 Isaac 似乎不活跃,但这里有一个可能的解决方案。
我的初始代码:
任务:删除除第一行之外的行。
我之前尝试过:
但没有得到预期的结果。
当我找到 meze 的帖子时,我将
for()
循环替换为do{} while()
:所以我得到了预期的结果。
Although the user Isaac seems to be inactive here comes a possible solution.
My initial code:
The task: removing the rows except the first.
What I had tried before:
And I didn't get the expected result.
When I found the post from meze, I replaced the the
for()
loop bydo{} while()
:and so I got the expected result.