获取封闭链接标签的 href 属性 [英] get href attribute of enclosing link tag
问题描述
这个有点麻烦.我需要一种使用 Jquery/JS 来查找封闭链接标签的 HREF 属性的方法:
Having a little trouble on this one. I need a way using Jquery/JS to find the HREF attribute of the enclosing link tag:
<a href="something.html"><img src="img1.jpg" class="active"></a>
我想按类定位 img 并找到前面第一个 href 属性的值.
I want to target the img by class and find the value of the 1st preceding href attribute.
$("img.active").somethingAwesome().attr("href");
请给我看一些Awesome() ...有帮助吗?
Please show me somethingAwesome() ...help?
推荐答案
$("img.active").parent("a").attr("href")
将得到直接parent 的 href 属性,假设它是一个锚点.如果图像和锚点之间有任何包含块的深度,请改用 $("img.active").closest("a").attr("href")
.
$("img.active").parent("a").attr("href")
will get the direct parent's href attribute, assuming it's an anchor. If there's any depth of containing blocks between the image and the anchor, use instead $("img.active").closest("a").attr("href")
.
这篇关于获取封闭链接标签的 href 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!