简体   繁体   English

隐藏不是特定类别的元素

[英]Hide element which is not a certain class

I want to hide an element which is not a certain class via jQuerys not() : 我想通过jQuerys not()隐藏不是特定类的元素:

 <a href="#" id="c_1" class="content-btn">Content 1</a>
 <a href="#" id="c_2" class="content-btn">Content 2</a>

 <div class="post-item c_1"></div>
 <div class="post-item c_2"></div> 

and

var thisContent;
jQuery('.content-btn').click(function() {
    thisContent = this.id;
    jQuery('.post_item').not('.'+thisContent).fadeOut();
}

am I using .not() method wrong in this context, because it seems not to work! 我在这种情况下使用.not()方法错误,因为它似乎不起作用!

Your selector needs to be 您的选择器必须是

jQuery('.post-item')

And you need to close the ) at the end of your jQuery, like this: 并且您需要在jQuery末尾关闭),如下所示:

var thisContent;
jQuery('.content-btn').click(function() {
    thisContent = this.id;
    jQuery('.post-item').not('.'+thisContent).fadeOut();
});

See http://codepen.io/anon/pen/EaNXjg for a working example. 有关工作示例,请参见http://codepen.io/anon/pen/EaNXjg

尝试使用

$(element).hasClass(".clasname").fadeOut();

as @AND Finally noticed modify your selector .. and while you use click on Anchor you need to use e.preventDefault; 作为@AND最后注意到修改您的选择器..在使用时单击Anchor,您需要使用e.preventDefault;。 try this 尝试这个

jQuery('.content-btn').click(function(e) {
    e.preventDefault;
    thisContent = this.id;
    jQuery('.post-item').not('.'+thisContent).fadeOut();
    $('.'+thisContent).fadeIn();
});

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM