How to use in jQuery :not and hasClass() to get a specific element without a class

后端 未结 5 1768
暖寄归人
暖寄归人 2021-01-31 01:08

I have this line of code:

$(\'#sitesAccordion .groupOfSites\').click(function() {
    var lastOpenSite = $(this).siblings().hasClass(\':not(.closedTab)\');
    c         


        
相关标签:
5条回答
  • 2021-01-31 01:51

    Use the not function instead:

    var lastOpenSite = $(this).siblings().not('.closedTab');
    

    hasClass only tests whether an element has a class, not will remove elements from the selected set matching the provided selector.

    0 讨论(0)
  • 2021-01-31 01:52

    It's much easier to do like this:

    if(!$('#foo').hasClass('bar')) { 
      ...
    }
    

    The ! in front of the criteria means false, works in most programming languages.

    0 讨论(0)
  • 2021-01-31 01:52

    You can also use jQuery - is(selector) Method:

    var lastOpenSite = $(this).siblings().is(':not(.closedTab)');
    
    0 讨论(0)
  • 2021-01-31 02:00

    jQuery's hasClass() method returns a boolean (true/false) and not an element. Also, the parameter to be given to it is a class name and not a selector as such.

    For ex: x.hasClass('error');

    0 讨论(0)
  • 2021-01-31 02:01

    I don't know if this was true at the time of the original posting, but the siblings method allows selectors, so a reduction of what the OP listed should work.

     $(this).siblings(':not(.closedTab)');
    
    0 讨论(0)
提交回复
热议问题