javascript - 我怎样才能只得到第一层的元素?

标签 javascript xml

所以,我有这个 xml

<conversation>
    <question>
        <title>I want to get this parent</title>
        <question>
            <title>I don´t want to get this one</title>
        </question>
        <question>
            <title>I don´t want to get this one</title>
        </question>
    </question>

    <question>and get this one</question>
    <question>and also this one too</question>
</conversation>

我想获得顶层,直接从 <conversation> 下降而不是从任何其他标签下降...我怎样才能只得到那些 3 <question>节点?

最佳答案

document.querySelectorAll('对话 > 问题')

the > 表示直子。

example here只是为了避免混淆:

console.log( 
	document.querySelectorAll('conversation > question')
)
<conversation>
  <question>
    <title>I want to get this parent</title>
    <question>
      <title>I don´t want to get this one</title>
    </question>
    <question>
      <title>I don´t want to get this one</title>
    </question>
  </question>

  <question>and get this one</question>
  <question>and also this one too</question>
</conversation>

关于javascript - 我怎样才能只得到第一层的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163246/

相关文章:

javascript - 也可以阻止在输入类型编号中输入负值的 Angular 方式(在复制粘贴和增加、减少时)

java - HtmlUnit JavaScript 回调 Java 方法

php - 长轮询 : Notifications in every browser

javascript - XMLHttpRequest 无法加载 "THIS URL"来源 http ://localhost:3000 is not allowed by Access-Control-Allow-Origin.

java - 有没有一种方法可以在 java 中读取 xml 文件而不必定义要读入的类

xml - 从xml拾取的Powershell联接对象

javascript - RadioButton 有值虽然没有选中

java - 一种布局中图像的最大尺寸是多少?

python - 如何使用 python lxml 通过 xslt 加速大型 xml 文件的转换

javascript - 更新数组时不会触发模型的更改事件?