admin管理员组文章数量:1391850
I've got a element tree that looks like.
<div>
<div required>
<div content></div>
</div>
<div>
<div content></div>
</div>
</div>
How can I do a querySelector on the root div that gets all elements tagged as "content" without getting any that are below a parent element with the "required" attribute? I'm thinking something like.
querySelectorAll('[content]:not([required]')
But that query would fetch all elements tagged as content and not tagged as required rather than all elements tagged as content that aren't below a required element.
I've got a element tree that looks like.
<div>
<div required>
<div content></div>
</div>
<div>
<div content></div>
</div>
</div>
How can I do a querySelector on the root div that gets all elements tagged as "content" without getting any that are below a parent element with the "required" attribute? I'm thinking something like.
querySelectorAll('[content]:not([required]')
But that query would fetch all elements tagged as content and not tagged as required rather than all elements tagged as content that aren't below a required element.
Share Improve this question asked Mar 5, 2020 at 3:04 user3875080user3875080 751 silver badge5 bronze badges 5- document.querySelectorAll(‘div:not([required]) [content]’) should get all elements which have the content attribute which aren’t descendants of an element with the required attribute. – Benjamin James Kippax Commented Mar 5, 2020 at 3:09
-
1
@BenjaminJamesKippax That won't work, because as long as any ancestor lacks the
required
attribute, that ancestor will match, and the child[content]
will be included. – CertainPerformance Commented Mar 5, 2020 at 3:18 - @CertainPerformance use the direct descendant selector then? :not([required]) > [content] – Benjamin James Kippax Commented Mar 5, 2020 at 3:20
-
@OP are your
content
elements only ever direct children of arequired
element, or can therequired
be some ancestor? – CertainPerformance Commented Mar 5, 2020 at 3:23 - @user3875080 Will you check to see if my solution works? – Miaplacidus Commented Mar 5, 2020 at 3:26
2 Answers
Reset to default 3Unfortunately, :not
only accepts a simple selector, so :not([required] [content])
to match a [content]
which is not a child of [content]
won't work. You'll have to programmatically filter the elements after selecting them:
const notRequiredContents = [...document.querySelectorAll('[content]')]
.filter(elm => !elm.closest('[required]'));
console.log(notRequiredContents);
<div>
<div required>
<div content></div>
</div>
<div>
<div content></div>
</div>
</div>
It'd be theoretically possible to do this with only a query string by chaining :not([required])
with the descendant selector, but it looks really ugly and repetitive and shouldn't be done:
const notRequiredContents = document.querySelectorAll(`
body > :not([required]) > [content],
body > :not([required]) > :not([required]) > [content],
body > :not([required]) > :not([required]) > :not([required]) > [content],
body > :not([required]) > :not([required]) > :not([required]) > :not([required]) > [content]
`);
// continue above pattern for as much nesting as may exist
console.log(notRequiredContents[0], notRequiredContents.length);
<div>
<div required>
<div content>a</div>
</div>
<div>
<div content>b</div>
</div>
</div>
Repl Example
JS
const query = document.querySelectorAll('div :not([required]) [content]')
query.forEach((element) => {
console.log(element.innerHTML)
})
HTML
<div>
<div required>
<div content>NO</div>
</div>
<div>
<div content>YES</div>
<div>
<div content>YES</div>
</div>
</div>
</div>
本文标签: javascriptquerySelectorAll() exclude attribute and children of that attributeStack Overflow
版权声明:本文标题:javascript - querySelectorAll() exclude attribute and children of that attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744770856a2624328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论