如何在jQuery中使用:not和hasClass()来获取没有类的特定元素 [英] How to use in jQuery :not and hasClass() to get a specific element without a class
本文介绍了如何在jQuery中使用:not和hasClass()来获取没有类的特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这行代码:
$('#sitesAccordion .groupOfSites')。click(function(){
var lastOpenSite = $(this).siblings()。hasClass(':not(.closedTab)');
console.log(lastOpenSite);
});
我得到false而不是获得其他元素之一(假设有一个 - 并且必须有)。我猜这个问题是:
.hasClass(':not(.closedTab)');
有什么问题?
我的目的是创建我自己的手风琴(不使用jQuery UI)
我试图像这样写:
点击(函数(){
//获取上次打开的选项卡
var lastOpenSite =
$('#sitesAccordion .groupOfSites' $(this).siblings()。hasClass(':not(.closedTab)');
//关闭上次打开的选项卡并添加class
lastOpenSite.hide()。toggleClass( 'closedTab');
//打开当前标签
$(this).children('。accordionContent')。toggle('fast');
//从打开的标签中移除类
$(this).toggleClass('closedTab');
});
这是最好的方法吗?
thanks,
Alon
解决方案
使用 not
函数:
var lastOpenSite = $(this).siblings()。not('。closedTab');
hasClass
仅测试元素是否具有类,不是
会从选定的集合中移除匹配提供的选择器的元素。
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:
.hasClass(':not(.closedTab)');
What is the problem?
My purpose is to create my own accordion (without using 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
解决方案
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中使用:not和hasClass()来获取没有类的特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文