所以,我有这个 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/