Can I put logical operators in document.querySelectorAll? If so, how?

后端 未结 1 2035
小鲜肉
小鲜肉 2021-02-13 04:44

Let\'s say I want to find all div elements and span inside p. Is it possible to get all what I want in a single `querySelectorAll\" invoca

相关标签:
1条回答
  • Yes. You can use the same logical operators allowed in CSS:

    OR: chain selectors with commas

    document.querySelectorAll('div, p span');
    // selects divs, and spans in ps
    

    AND: chain selectors without whitespace

    document.querySelectorAll('div.myClass');
    // selects divs with the class "myClass"
    

    NOT: :not()-selector

    document.querySelectorAll('div:not(.myClass)');
    // selects divs that do not have the class "myClass"
    
    0 讨论(0)
提交回复
热议问题