问题描述
对此有一点麻烦.我需要一种使用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");
请给我看somethingAwesome()...帮助?
Please show me somethingAwesome() ...help?
推荐答案
$("img.active").parent("a").attr("href")
将获得直接父级的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属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!