如果在没有ID或类的另一个特定标记(子)下找到特定标记,则隐藏特定标记

Hide specific tag if found under (child of) another specific tag without ID or class

本文关键字:隐藏 另一个 如果 ID      更新时间:2023-09-26

我试图隐藏一个特定的标签('路径'标签),属于另一个特定的标签('文本'标签)-问题是这些标签都没有给ID或类名(我不能给他们一个自己)。另一个问题是,有许多"文本"标签与其他孩子,我无法隐藏。

下面是(简化的)代码:

<text>
    <tspan> <!-- must not hide this child -->
</text>
<text>
    <path> <!-- Need to hide this child -->
</text>

这些标签的唯一属性是样式属性。

我一直在尝试不同的方法来隐藏这个子元素,但我似乎找不到一种方法来专门隐藏'path'子元素,只有没有id或类。

我将非常感谢任何帮助。谢谢!

如果你知道你想要隐藏哪一个,你可以使用。

假设你的文档是:

<html>
  <body>
    <text>
      <tspan> <!-- must not hide this child -->
    </text>
    <text>
      <path> <!-- Need to hide this child -->
    </text>
  </body>
</html>
var child = document.body.children[1];
child.style.display = "none";

或者您可以简单地遍历'子'数组并匹配"path"元素,以知道何时需要隐藏它