1. The first form - $("#document a").not(".menu a").dostuff() - should
work. I think that this is a bug that it doesn't, and should be reported
as such.

I don't think it's a bug, and I'm not surprised this selector doesn't work.. This is first selecting all <a> elements that are descendants of #document. Then, from that set of matching <a> elements, it's trying to remove all that have a class of "menu" and a descendant <a>. So, it should select all of the <a> elements.

If you can rely on the "menu" class being a child of #document, you can do this:

$('#document > :not(.menu)').find('a').dostuff();

2. This does work:
$("#document a").not( $(".menu a") ).dostuff();

Yep, that looks like a pretty elegant solution. Nice one, Brian!

This would also work, but differently:

$('#document a').filter(function() {
  return !$(this).parents('.menu').length;
}).dostuff();

Or, a little differently still, you could do stuff with .each():

$('#document a').each(function() {
  if( !$(this).parents('.menu').length) {
    dostuff();
  }
});

Cheers,

--Karl
_________________
Karl Swedberg
www.englishrules.com
www.learningjquery.com



On Jun 7, 2007, at 9:19 AM, Brian Miller wrote:




2. This does work:
$("#document a").not( $(".menu a") ).dostuff();

Instead of removing the elements by expression, you're doing a second
selection, and removing the secondly selected elements explicitly.

- Brian


Hi All,

I've googled and googled to no avail, this question has too many
instances of words like "not" in it and I'm struggling to find
anything useful.

I'm doing something quite complex but the crux of the question is
this, with a structure like so:

<div id="document">
     <ul class="menu">
          <li><a href="">Menu Link</a></li>
          <li><a href="">Menu Link</a></li>
          <li><a href="">Menu Link</a></li>
     </ul>
     <p><a href="">Paragraph Link</a></p>
     <p><a href="">Paragraph Link</a></p>
     <p><a href="">Paragraph Link</a></p>
</div>

I want to select all anchor tags but _not_ the ones in the menu. Now I
know in the situation above I could just do:

$("#document p a").dostuff();

But imagine I have a document where the menu might be mucked about
with and the content has all kinds of things going on. What I really
want to do is something like:

$("#document [any element path that doesn't include .menu]
a").dostuff();

I have tried:

$("#document a").not(".menu a").dostuff();
$("#document *:not(.menu) a").dostuff();
$("#document a:not(.menu a)").dostuff();

I am at a loss. No doubt there is a really simple way of saying
"select all of the elements where none of the parents are the .menu
element".

Thanks for your help.

James





Reply via email to