使用 childNodes 处理子节点 javascript 时出现问题

发布于 2024-11-26 12:09:55 字数 685 浏览 1 评论 0原文

我试图让 div 的所有子节点都不显示。它与 getElementsByTagname('*') 配合良好。

我的标记

<div id="container">
    <div id="child1"></div>
    <div id"child2">
        <div id="inner-child"></div>
    </div>
</div>

我想仅操作 child1、child2 的显示属性。

      function hideAllChildren(){
        var elem = document.getElementById("container");
        var children = elem.childNodes;
        alert("children " + children.length)
        for (i=0; i < children.length ;i++)
        {
        children[i].style.display="none";// Error - children[i].style undefined
        }

      }

你能找出问题所在吗?

I am trying to make display none for all the child nodes of a div. It works well with getElementsByTagname('*')

My Markup

<div id="container">
    <div id="child1"></div>
    <div id"child2">
        <div id="inner-child"></div>
    </div>
</div>

I would like to manipulate the display property of only the child1, child2.

      function hideAllChildren(){
        var elem = document.getElementById("container");
        var children = elem.childNodes;
        alert("children " + children.length)
        for (i=0; i < children.length ;i++)
        {
        children[i].style.display="none";// Error - children[i].style undefined
        }

      }

Can you figureout what the issue could be ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你的背包 2024-12-03 12:09:55

并非所有子节点都是元素,有些是某些浏览器中的文本节点,并且文本节点没有样式属性。尝试访问不存在的属性的属性会引发错误。

首先测试节点类型或节点是否具有(其非 false 值)样式属性:

  if (children[i].style) {
    children[i].style.display="none";
  }

但是,您可能会发现使用类和适当的 CSS 规则并将其添加到父元素会更好。

例如

<style type="text/css">

.hideAll * {
  display: none;
}

</style>

</script type="text/javascript">

<button onclick="
  document.getElementById('d0').className = 'hideAll';
">Hide all</button>
<button onclick="
  document.getElementById('d0').className = '';
">Show all</button>

<div id="d0">Here is the div
  <ul>
    <li class="item">apple
    <li class="item">orange
    <li class="item">banana
  </ul>
</div>

Not all the child nodes are elements, some are text nodes in some browsers and text nodes don't have a style property. Trying to access a property of a non-existant property throws an error.

Either test the node type or that the node has a (non-falsey value for its) style property first:

  if (children[i].style) {
    children[i].style.display="none";
  }

However, you may find it much better to use a class and appropriate CSS rule and just add it to the parent element.

e.g.

<style type="text/css">

.hideAll * {
  display: none;
}

</style>

</script type="text/javascript">

<button onclick="
  document.getElementById('d0').className = 'hideAll';
">Hide all</button>
<button onclick="
  document.getElementById('d0').className = '';
">Show all</button>

<div id="d0">Here is the div
  <ul>
    <li class="item">apple
    <li class="item">orange
    <li class="item">banana
  </ul>
</div>
情定在深秋 2024-12-03 12:09:55

为什么要隐藏所有子节点。

您可以在其中隐藏父级,所有子级都会自动隐藏。

所以它会很简单:

function hideAllChildren(){
  var elem = document.getElementById("container");
  //alert("children " + children.length)
  elem.style.display="none";
}

Why do you want to hide all the child nodes.

Where you can hide the parent and all the child will automatically hide.

So it will be simply:

function hideAllChildren(){
  var elem = document.getElementById("container");
  //alert("children " + children.length)
  elem.style.display="none";
}
2024-12-03 12:09:55

试试这个

<div id="container">container
    <div id="child1">child1</div>
    <div id"child2">Child 2
        <div id="inner-child">inner-child</div>
    </div>
</div>
<div id="clickme">Click me</div>

/// java 脚本

$('#clickme').click(function() {
  hideAllChildren();
});

    function hideAllChildren(){
        var elem = document.getElementById("container");
        var children = elem.childNodes;
        alert("children " + children.length)
            $('#container').hide();      

      }

try this

<div id="container">container
    <div id="child1">child1</div>
    <div id"child2">Child 2
        <div id="inner-child">inner-child</div>
    </div>
</div>
<div id="clickme">Click me</div>

/// java script

$('#clickme').click(function() {
  hideAllChildren();
});

    function hideAllChildren(){
        var elem = document.getElementById("container");
        var children = elem.childNodes;
        alert("children " + children.length)
            $('#container').hide();      

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