jQuery Select Element that has no Class or ID

后端 未结 3 852
时光说笑
时光说笑 2020-12-21 07:41

Trying to find a selector to get all elements that have both no class, and no id set to them.

So far I have 2 different outputs depending on if there is a space in

相关标签:
3条回答
  • 2020-12-21 08:27
    $('*:not([id]):not([class])')
    

    Your withSpace will select elements that don't have a class and have a parent which don't have an id. Not sure about noSpace. UPDATE Oh, it's does the same thing as mine, actually. So, the answer is your last selector.

    0 讨论(0)
  • 2020-12-21 08:28

    withSpace - $('*:not([id]) *:not([class])'); will find all elements with no class that are inside an element without an ID. Putting a space in the selector is like calling find seperately.

    You could change noSpace to be this instead and still get the right result:

    var noSpace= $('*:not([id]):not([class])'); // second * not needed
    

    JSFiddle used for testing

    0 讨论(0)
  • 2020-12-21 08:34
    $("*").not("[class],[id]") 
    

    You can keep adding attributes or classes or tags by a comma seperator

    Additional notes http://api.jquery.com/not-selector/

    The .not() method will end up providing you with more readable selections than pushing complex selectors or variables into a :not() selector filter. In most cases, it is a better choice

    (:not) is faster since it is css3 selector browser but show a real slow query in older browsers ie8 or the browsers that dont support css3

    http://jsperf.com/jquery-css3-not-vs-not test results

    0 讨论(0)
提交回复
热议问题