我有这样的设置.
var element = $(".module-panel", this.el); $('.module-panel:not('+element+')').hide();我想做的是用.module-panel类隐藏所有div,除了存储在var元素中的那个.
What I'm trying to do is hide all divs with the class .module-panel except the one that is stored inside the var element.
推荐答案最简单的方法是使用.not()
The easiest way to do this is with .not()
var element = $(".module-panel", this.el); $('.module-panel').not(element).hide();也完全可以使用:not(),尽管差异很小,但是如果要匹配多个元素,可能会对CSS3选择器有所帮助: not().
Using :not() is entirely possible as well, and although the difference is miniscule, if there are multiple elements you're matching against, it could make a difference for the favorable CSS3 selector :not().
jQuery创建一个方法 .not()来过滤传递给它的任何元素. CSS3有一个伪选择器:not(),它允许浏览器完成工作,使其速度稍快,但功能却不那么丰富.
jQuery creates a method .not() which filters out any element passed to it. CSS3 has a pseudo selector :not() which allows the browser to do the work, making it slightly faster, but not as versatile.
此处的测试结果: jsfiddle/iredmedia/jPNYK/
查询不起作用的原因是因为您将jQuery对象包含到查询字符串中.为了使您的查询有效,您必须传递一个特定元素ID/类 string ,如
The reason your query doesn't work is because you are concattenating a jQuery object into a query string. In order to make your query work, you must pass it a specific element id/class string, as in
var element = '.doNotReturn', excluded = $('#eltFamily:not(' + element + ')').nextMethod();希望这可以帮助您了解:not()vs .not();
Hope this helps you understand :not() vs .not();
更多推荐
使用jQuery不是运算符
发布评论