隐藏不是某个 class 的元素

Hide element which is not a certain class

我想通过 jQuerys 隐藏一个不是特定 class 的元素 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> 

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

我在这种情况下使用 .not() 方法是不是错了,因为它似乎不起作用!

您的选择器需要

jQuery('.post-item')

并且您需要关闭 jQuery 末尾的 ),如下所示:

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

有关工作示例,请参阅 http://codepen.io/anon/pen/EaNXjg

尝试使用

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

as @AND 最后注意到修改你的选择器..当你使用点击锚时你需要使用e.preventDefault;试试这个

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

DEMO