开发者

Select class not contained in div

Is it possible to select a class of a page without selecting the same class which were encapsulated in a special div?

something like:

$(!".container").$(".selectedClass").dosomething..

or

$(".selectedClass").parent(!"container").dosomething..

Here's some example...

<div class="blah">
    <div class="remove"><div>
</div>
<div class="blaha">
    <div class="remove"><div>
</div>
<div class="dont_remove_remo开发者_开发问答ve_here">
    <div class="remove"><div>
</div>
<div class="blahblah">
    <div class="remove"><div>
</div>

I want to remove all classes "remove" but not in the "dont_remove_remove_here" container.


I would never use the other 'div:not(.container)' answers on this page in production code because it would lead to extremely slow performance.

The code below is much more efficient:

      $(".remove").each(function(i,elt) {
        if (!$(elt).parent().hasClass("dont-remove")) {
           $(elt).remove();
        }
      });


If I understand correctly this should work, but it will be quite slow. Plus you may want to change div to something else, or further refine the first part.

$("div:not(.container)").find(".selectedClass")


And the answer is..

$("div:not(.container)").remove(".remove");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜