动态删除 DOM 表中的行

发布于 2024-10-12 22:13:02 字数 4068 浏览 6 评论 0原文

我在下面的循环中遇到了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜访吸血鬼 2024-10-19 22:13:02

getElementsByTagName 返回一个 NodeList,正如 W3C 所说:“DOM 中的 NodeList 对象是活动的。”。这意味着当您从 DOM 树中删除节点时,它也会从 nodes 列表变量中删除。因此,您需要始终删除列表的第一个元素:

var node = nodes[0];
if (univArray[z].ownership != "public") {
    node.parentNode.removeChild(node)
}

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 your nodes list variable. So you need always remove the first element of your list:

var node = nodes[0];
if (univArray[z].ownership != "public") {
    node.parentNode.removeChild(node)
}
夏了南城 2024-10-19 22:13:02

感谢您的帮助。我尝试遵循您的建议,但 if 语句似乎被忽略了。这是我现在所拥有的:

function filter(){
   if (document.getElementById("public").checked) {
      var nodes = document.body.getElementsByTagName('tr')
      for (var z = 0; z <  nodes.length; z++) {
          var node = nodes[1];
          if (univArray[z].ownership != "public") {
              node.parentNode.removeChild(node)
          }
      }
   }
   if (document.getElementById("private").checked) {
      var nodes = document.body.getElementsByTagName('tr')
      for(var i=0; i <univArray.length; i++ ) {
          //Don't want to delete header row so we start at 1
          var node= nodes[1];
          if (univArray[i].ownership != "private") {
              node.parentNode.removeChild(node)
          }     
      }
   }
}

Thanks for your help. I tried following your suggestion, but the if statement seems to be getting ignored. Here's what I have now:

function filter(){
   if (document.getElementById("public").checked) {
      var nodes = document.body.getElementsByTagName('tr')
      for (var z = 0; z <  nodes.length; z++) {
          var node = nodes[1];
          if (univArray[z].ownership != "public") {
              node.parentNode.removeChild(node)
          }
      }
   }
   if (document.getElementById("private").checked) {
      var nodes = document.body.getElementsByTagName('tr')
      for(var i=0; i <univArray.length; i++ ) {
          //Don't want to delete header row so we start at 1
          var node= nodes[1];
          if (univArray[i].ownership != "private") {
              node.parentNode.removeChild(node)
          }     
      }
   }
}
浮光之海 2024-10-19 22:13:02

尽管用户 Isaac 似乎不活跃,但这里有一个可能的解决方案。

我的初始代码:

 <table align="center" border="1" cellpadding="15" cellspacing="10" >
 <tr>
   <th colspan="7"> Headline </th>
 </tr>
 <tr>
   <td>colmun</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
 </tr>
 <tr>
   <td>colmun</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
 </tr>     
 </table>

任务:删除除第一行之外的行。

我之前尝试过:

var tableObj = document.getElementsByTagName("table")[0];
var coloumns = tableObj.getElementsByTagName("tr");
for(var i=0; i < coloumns.length; i++ )
{
  coloumns[i].parentNode.removeChild(coloumns[i]);
}

但没有得到预期的结果。

当我找到 meze 的帖子时,我将 for() 循环替换为 do{} while()

var tableObj = document.getElementsByTagName("table")[0];
var coloumns = tableObj.getElementsByTagName("tr");
do {
  coloumns[0].parentNode.removeChild(coloumns[0]);
} while (coloumns.length > 1)

所以我得到了预期的结果。

Although the user Isaac seems to be inactive here comes a possible solution.

My initial code:

 <table align="center" border="1" cellpadding="15" cellspacing="10" >
 <tr>
   <th colspan="7"> Headline </th>
 </tr>
 <tr>
   <td>colmun</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
 </tr>
 <tr>
   <td>colmun</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
   <td>column</td>
 </tr>     
 </table>

The task: removing the rows except the first.

What I had tried before:

var tableObj = document.getElementsByTagName("table")[0];
var coloumns = tableObj.getElementsByTagName("tr");
for(var i=0; i < coloumns.length; i++ )
{
  coloumns[i].parentNode.removeChild(coloumns[i]);
}

And I didn't get the expected result.

When I found the post from meze, I replaced the the for() loop by do{} while():

var tableObj = document.getElementsByTagName("table")[0];
var coloumns = tableObj.getElementsByTagName("tr");
do {
  coloumns[0].parentNode.removeChild(coloumns[0]);
} while (coloumns.length > 1)

and so I got the expected result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文