是否始终将a.href属性返回为绝对值? [英] Is the a.href property always returned as absolute?

查看:74
本文介绍了是否始终将a.href属性返回为绝对值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下HTML中:

<a id="link" href="page.htm">Page</a>

我发现 document.getElementById("link").href 始终将绝对路径返回到 page.htm ,而不是我键入的相对路径它.参见 http://jsfiddle.net/4HgAW/.

I'm finding that document.getElementById("link").href always returns the absolute path to page.htm rather than the relative path as I have typed it. See http://jsfiddle.net/4HgAW/.

我可以保证在所有浏览器中javascript都将始终返回绝对路径吗?

我要问的原因是我要抓住 http://www .... 部分来检查链接指向的域,并且我需要它在内部工作链接.

The reason I'm asking is that I'm grabbing the http://www.... part to check which domain the link points to, and I need it to work for internal links as well.

推荐答案

是的,所有相关的浏览器都返回完全限定的URL.

Yes, all relevant browsers return the fully qualified URL.

如果要检索 href 属性的原始值(在本例中为'page.html'),可以执行以下操作:

If you want to retrieve the original value of the href attribute ('page.html' in this case), you can do this:

anchor.getAttribute('href')

但是,在旧版本的IE(8及更低版本)中,这似乎不起作用.

However, that doesn't seem to work in older versions of IE (8 and below).

实时演示: http://jsfiddle.net/simevidas/4HgAW/1/

这篇关于是否始终将a.href属性返回为绝对值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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