我有一行代码:

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

What is the problem?

我的目的是创建自己的手风琴(不使用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

推荐答案

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.

Jquery相关问答推荐

:eq Jquery,我似乎无法显示第一个之后的元素

如何在没有实体框架的情况下在 ASP.NET 上使用 ajax 和 jquery 从列表创建数据表

ASP.NET MVC - Ajax 将空值传递给控制器

用 jQuery/JavaScript 检测数字或字母?

如何使用 jquery 获取屏幕的高度

纯css关闭按钮

jQuery 与 ExtJS

在 `click` 和 `enter` 上触发事件

了解 Backbone.js REST 调用

Facebook 风格的 JQuery 自动完成插件

JQuery 仅在 Rails 4 应用程序中的页面刷新时加载

jQuery Keypress 箭头键

如何使用 jQuery 获取 div 的当前类?

动态设置 iframe src

jquery click 不适用于 ajax 生成的内容

jQuery-UI 的自动完成不能很好地显示,z-index 问题

我可以限制 JavaScript 中数组的长度吗?

为什么 Chrome 会忽略本地 jQuery cookie?

如何阻止 Chrome 进入调试模式?

Twitter Bootstrap alert 消息关闭并再次打开