admin管理员组

文章数量:1026415

I need to select all divs of a certain class (jqx-slider) excluding one ID (#str_prg) - something like:

$("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($(this).attr('id') ! = "str_prg"){

}

Thanks!

I need to select all divs of a certain class (jqx-slider) excluding one ID (#str_prg) - something like:

$("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($(this).attr('id') ! = "str_prg"){

}

Thanks!

Share Improve this question asked Feb 26, 2014 at 0:51 user2723490user2723490 2,1004 gold badges28 silver badges37 bronze badges 1
  • the documentation for .not is pretty clear... api.jquery./not-selector – Jorg Commented Feb 26, 2014 at 0:57
Add a ment  | 

3 Answers 3

Reset to default 4

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

Try to remove an unnecessary space char like this:

$("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.

I need to select all divs of a certain class (jqx-slider) excluding one ID (#str_prg) - something like:

$("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($(this).attr('id') ! = "str_prg"){

}

Thanks!

I need to select all divs of a certain class (jqx-slider) excluding one ID (#str_prg) - something like:

$("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($(this).attr('id') ! = "str_prg"){

}

Thanks!

Share Improve this question asked Feb 26, 2014 at 0:51 user2723490user2723490 2,1004 gold badges28 silver badges37 bronze badges 1
  • the documentation for .not is pretty clear... api.jquery./not-selector – Jorg Commented Feb 26, 2014 at 0:57
Add a ment  | 

3 Answers 3

Reset to default 4

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

Try to remove an unnecessary space char like this:

$("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.

本文标签: javascriptjQuery select all classes except one IDStack Overflow