简体   繁体   English

jQuery选择除一个ID之外的所有类

[英]jQuery select all classes except one ID

I need to select all divs of a certain class (jqx-slider) excluding one ID (#str_prg) - something like: 我需要选择某个类(jqx-slider)的所有div,但不包括一个ID(#str_prg)-类似:

$("div.jqx-slider :not(#str_prg)").each(function () {
.....
});

What is the correct syntax for that? 正确的语法是什么?

Also, would it be faster and more effecient code, if I add a "if" condition inside the loop - like 另外,如果我在循环中添加“ if”条件,它将是更快,更有效的代码吗?

if($(this).attr('id') ! = "str_prg"){

}

Thanks! 谢谢!

You are using an descendant selector between the class selector and the not selector, which is invalid for your requirement 您在类选择器和非选择器之间使用后代选择器,这对您的要求无效

$("div.jqx-slider:not(#str_prg)")

when you say $("div.jqx-slider :not(#str_prg)") it selects all descendants of elements with class jq-slider except the one with id str_prg 当您说$("div.jqx-slider :not(#str_prg)")它将选择jq-slider类的所有元素的后代,但ID为str_prg的元素的后代

尝试像这样删除不必要的空格字符:

$("div.jqx-slider:not(#str_prg)")

Remove the space, as it would cause you to select children, instead of the element itself. 删除空格,因为它将导致您选择子项,而不是元素本身。

$("div.jqx-slider:not(#str_prg)").each(function() {
    .....
});

For the second part of your question, it would be better to just use the CSS selector instead of a JS loop. 对于问题的第二部分,最好只使用CSS选择器而不是JS循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM