如何在jQuery:not和hasClass()中使用来获得没有类的特定元素

时间:2023-01-22 20:35:11

I have this line of code:

我有这行代码:

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

I get "false" instead of getting one of the other elements (assuming that there is one - and there must be). I guess the problem is with:

我得到的是“false”而不是其他元素之一(假设有一个——而且一定有)。我想问题在于:

.hasClass(':not(.closedTab)');

What is the problem?

这个问题是什么?

My purpose is to create my own accordion (without using jQuery UI)

我的目的是创建我自己的手风琴(不使用jQuery UI)

and I am trying to write it like this:

我试着这样写:

$('#sitesAccordion .groupOfSites').click(function() {

    //Get the last opened tab
    var lastOpenSite = $(this).siblings().hasClass(':not(.closedTab)');

    //Close last opened tab and add class
    lastOpenSite.hide().toggleClass('closedTab');

    //Open the current Tab
    $(this).children('.accordionContent').toggle('fast');

    // remove class from open tab
    $(this).toggleClass('closedTab');


});

Is this the best way? thanks, Alon

这是最好的方法吗?谢谢,阿龙

5 个解决方案

#1


95  

Use the not function instead:

使用not函数代替:

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.

hasClass只测试元素是否具有类,而不会从与提供的选择器匹配的选定集合中删除元素。

#2


73  

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.

!在标准前面意味着false,在大多数编程语言中都适用。

#3


7  

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.

jQuery的hasClass()方法返回布尔(true/false),而不是元素。另外,要给它的参数是类名,而不是选择器本身。

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

为例:x.hasClass('错误');

#4


3  

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

您也可以使用jQuery - is(selector)方法:

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

#5


1  

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.

我不知道这在最初发布时是否正确,但是sibling方法允许选择器,因此减少了OP列出的内容。

 $(this).siblings(':not(.closedTab)');

#1


95  

Use the not function instead:

使用not函数代替:

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.

hasClass只测试元素是否具有类,而不会从与提供的选择器匹配的选定集合中删除元素。

#2


73  

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.

!在标准前面意味着false,在大多数编程语言中都适用。

#3


7  

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.

jQuery的hasClass()方法返回布尔(true/false),而不是元素。另外,要给它的参数是类名,而不是选择器本身。

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

为例:x.hasClass('错误');

#4


3  

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

您也可以使用jQuery - is(selector)方法:

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

#5


1  

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.

我不知道这在最初发布时是否正确,但是sibling方法允许选择器,因此减少了OP列出的内容。

 $(this).siblings(':not(.closedTab)');