使用jQuery不是运算符

编程入门 行业动态 更新时间:2024-10-26 00:27:19
本文介绍了使用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.

此处的测试结果: 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不是运算符

本文发布于:2023-11-22 03:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!