我想通过 jQuery 隐藏一个不属于某个类的元素 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一个工作示例。
关于javascript - 隐藏不属于某个类的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755334/