views:

1423

answers:

4

Hi, Please help... How can I code at Jquery, "Selecting a div's links but not child divs' links of that div? Thanks. Best Regards.

+6  A: 

use the direct child selector >

e.g

$('div>a')

UPDATE: to appease Mr RoBorg

If you do have anchors inside nested elements inside the first div then the following will work. This uses a filter to ensure that the parent div is indeed the div you are targetting rather than a nested div.

Demo

  var firstDivAnchors = $('div a').filter( function(){
        return $(this).closest('div').is('#yourDiv');
  });
redsquare
Not quite... this won't get <div><span><a>... which the question suggests it should
Greg
he said a divs links, not a div spans links.
redsquare
A: 

If there is a specific div you want to target, you can try:

$("div#mydiv a :not(div a)");

EDIT: It would be great if you post some more context (HTML) :)

Danita
that will always give 0 results
redsquare
It is working on my site (www.viajeros.com). Try it in firebug: $("div#footer a :not(div a)");
Danita
I think it works by virtue of CSS selectors weight: "div#something" weights more than only "div"
Danita
see http://pastebin.me/496c817fe0c7e
redsquare
@Danita - you need to check your markup at w3c, many errors (240+) which will mean strange selector results
redsquare
your #footer does in fact have 0 anchor tags without being in child divs, your above selector reports 2
redsquare
@redsquare Special care has been taken to conform to w3c recommendations. Also we are checking our pages with HTML Tidy for Firefox. We have 20 warnings (not 240+, not errors) in the homepage, most of which are from code we're required to keep verbatim and for special markup for WAI-ARIA.
Danita
@redsquare As for the specific question of this thread, you're right. I was wrong.
Danita
@Danita go here http://tinyurl.com/98g2u7 your site has 247 Errors, 29 warning(s)....
redsquare
+1  A: 

I don't remember quite well, but doesn't children() does this work?

$(thisDiv).children('a')

In docs.jquery.com it reads: children() Get a set of elements containing all of the unique immediate children of each of the matched set of elements.

Ricardo Vega
Yeah children or > is the same - Direct Children
redsquare
true, but as far as I know the difference is that using children doesn't use a regex to deconstruct the selector, but both are good approaches.
Ricardo Vega
A: 

[It's solved] Thanks too much to everybody... Especially to Redsquare... You solved my problem. Best regards to everybody.

tick my answer!!
redsquare