How do I select all the links that has an immediate img tag in it using jQuery?
I have the following html,
&l开发者_运维问答t;div id="sec">
<a><img /></a>
</div>
how do I select all the links that contains an immediate img tag in it in this div with id="sec" using jQuery?
div#sec a > img
The >
means immediate descendant.
Use .parent()
after the fact to get the <a>
again.
$('#sec a>img').parent()
could work. Haven't tested it.
$('#sec a:has(> img)')
See :has selector
You could even use jQuery.fn.has
.
$('a').has('img');
or jQuery.fn.find
in hand together with jQuery.fn.end
:
$('a').find('img').end();
精彩评论