获取包含链接标签的href属性 [英] get href attribute of enclosing link tag

查看:80
本文介绍了获取包含链接标签的href属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此有一点麻烦.我需要一种使用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属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆