使用jQuery不是运算符 [英] using jquery not operator

查看:80
本文介绍了使用jQuery不是运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的设置.

        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.

此处的测试结果: http://jsfiddle.net/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不是运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆