I want to be able to use querySelectorAll
to query all i
elements which does not have a font
tag inside of it. I have tried the following selector, but it is not working:
document.querySelectorAll('i > *:not(font)')
<i>
<font color="008000">Here goes some text</font>
</i>
<br />
<i>Here goes another text</i>
3
Answers
The code in question doesn’t select all
i
elements which do not contain afont
element like you intend. It actually selects all children ofi
that are notfont
elements.For the behaviour you want, you’ll need to use the new
:has()
selector in CSS which is available in all major browsers (except behind a flag in firefox):It should work.
Because you’re spreading the NodeList that
querySelectorAll()
returns into an array with[...]
, we can just use array standard methods to remove DOM nodes from it: