Traversing to the last child element in a tree using jQuery selector
<div id="myDIV">
<div>
<span>
<a href="#">Seek me!</a>
</span>
</div>
</div>
How can I find A tag using jQuery selector (not looping through children()) If I know only myDIV id.
Well, it really sounds a bit awkward. For example I've clicked #myDIV and i need to get text f开发者_如何转开发rom the last child tag. It could A, span, div, p, whatever. Also myDIV could not even have any children elements.
Excuse me my English
Like this:
$("#myDIV :not(:has(*))")
This will find all the last "leaf" elements, you could restrict to only <a>
, etc if you wanted. For your markup:
$("#myDIV :not(:has(*))").text() // "Seek me!"
$("#myDIV :not(:has(*))").length // 1
:empty
doesn't work here (because it has a text node child), but finding things with no child elements will, here's an example page showing this.
It's just $('#myLink')
精彩评论