如何在 jQuery 中使用 :not 和 hasClass() 来获取没有类的特定元素 [英] How to use in jQuery :not and hasClass() to get a specific element without a class

查看:79
本文介绍了如何在 jQuery 中使用 :not 和 hasClass() 来获取没有类的特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这行代码:

$('#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:

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

有什么问题?

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

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

我正在尝试这样写:

$('#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

推荐答案

使用not 函数代替:

Use the not function instead:

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

hasClass 只测试一个元素是否有一个类,not 会从与提供的选择器匹配的选定集合中删除元素.

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

这篇关于如何在 jQuery 中使用 :not 和 hasClass() 来获取没有类的特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆