例如,
var relevantItems = ul.querySelectorAll('li.someClass[data-x=someData]')
只应匹配来自外部的这些元素,引用<ul>
:
<ul>
<!-- This <li> matches -->
<li class="someClass" data-x="someData">
<ul>
<!-- depending on the query, there may be some extra matches within the hierarchy, but these should not be searched or included in the results -->
</ul>
</li>
<!-- need to also includes additional matches like this one: -->
<li class="someClass" data-x="someData">
</li>
</ul>
现在我有几个选择:
使用JavaScript循环遍历childNodes
/ children
数组并传统上比较className
和dataset.x
变量。
为每个<ul>
提供自己的id
并使用#id>
前缀功能,如下所示:
ul.querySelectorAll('#' + ul.id + ' > li....')
是否有更简单的语法或替代功能,例如以某种方式过滤childNodes
列表,以实现相同的查询? (不添加外部库即jQuery的)