使用Javascript获取图像的href属性 [英] Getting the href attribute of an image with Javascript

查看:81
本文介绍了使用Javascript获取图像的href属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript的新手,真的需要一些帮助!

New to Javascript, really need some help!

现在我在HTML页面中有一个图像,如下所示:

Now I have an image in a HTML page, like this:

<a class="p" href="http://www.abc.com"><img src="http://www.abc.com/logo.jpg" alt="" /></a>

并通过以下方式获取图像元素:

And get the image element by:

var e.document.elementFromPoint(x,y);

当我单击图像时,可以通过以下方式成功获取src属性或offset属性:

When I clicked on the image, I can get the src attribute or offset attributes successfully by:

e.src or e.offsetHeight

但是,当我使用它时,它会返回NULL

However, it returns NULL when I use:

return e.href;

那么我如何获得正确的href属性(http://www.abc.com)??

So how can I get the correct href attribute (http://www.abc.com) ??

谢谢

峰值

推荐答案

href不是图像的属性,而是A元素.

The href is not a propery of the image but of the A element.

您可以通过使用图像的 .parentNode 属性来访问它.因为它是它的直接父代.

You can acces it by using the .parentNode propery of the image. as it is its direct parent.

这篇关于使用Javascript获取图像的href属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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