开发者

jQuery selector to find link and all element inside

开发者 https://www.devze.com 2023-02-07 16:16 出处:网络
I need to select any links within a div e.g. div abc --- to finally unbind its click event. <div id=abc>

I need to select any links within a div e.g. div abc --- to finally unbind its click event.

<div id=abc> 
<a href=google.com>first link</a> 
<a href=google.com><img> src=google.png/>second link</a> 
<a href=google.com><span>xxxxx</span> second link</a> 
</div>

I use below selector

$('#abc a')

but it only work if the hyperlink only has text inside 开发者_如何学运维(first example above) -- if i put span or image (second and third examples) -- i still can click the hyperlink. In other words, above jquery selector did not select its children.

What's the right jQuery in above?

Thanks so much.


$('#abc a') only selects a elements. If you want to select a elements and all descendants, this selector does the job, though it's not the most efficient:

$('#abc a, #abc a *')

If you don't mind doing it in two steps, you can make it a bit quicker:

var $links = $('#abc a');
$links.add($links.find('*'));
0

精彩评论

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