开发者

Finding all divs with a link which has a specific id in jquery

I'm looking for a way to select the all the divs which has a sub link element with a specific id.

Structure

<div>
开发者_如何学Go<a href="#" id="123">link</a>
</div>

How do I select the div with jquery?


You can use the :hasdocs selector.

$('div:has(>#123)')

as mentioned, you should not use numbers for ids. You can prefix them with something like id_ and remove that when you want the actual number.


update

added the > to the selector used by the :has to only select the div that has the link as a direct child.


$("#link-123").closest("div")

will get you the parent. I'd suggest giving the div a class so that, in case you have to insert an intervening div later, the code works reliably. So it'd be something like:

$("#link-123").closest(".link-container")
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜