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 :has
docs 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")
精彩评论