Html 从锚点 (a) 标签获取本地 href 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15439853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get local href value from anchor (a) tag
提问by Michael Plautz
I have an anchor tag that has a local href value, and a JavaScript function that uses the href value but directs it to a slightly different place than it would normally go. The tag looks like
我有一个具有本地 href 值的锚标记,以及一个使用 href 值但将其定向到与通常情况略有不同的位置的 JavaScript 函数。标签看起来像
<a onclick="return follow(this);" href="sec/IF00.html"></a>
and a JavaScript function that looks like
和一个看起来像的 JavaScript 函数
baseURL = 'http://www.someotherdomain.com/';
function follow(item) {
location.href = baseURL + item.href;
}
I would expect that item.href
would just return a short string of "sec/IF00.html", but instead it returns the full href, "http://www.thecurrentdomain.com/sec/IF00.html". Is there a way that I can pull out just the short href as put in the anchor <a>
tag? Or do I lose that by natural HTML behavior?
我希望item.href
它只返回一个短字符串“sec/IF00.html”,而是返回完整的 href,“http://www.thecurrentdomain.com/sec/IF00.html”。有没有办法可以只提取锚<a>
标签中的短href ?或者我会因为自然的 HTML 行为而失去它吗?
I suppose I could use a string manipulation to do this, but it gets tricky because my local page may actually be "http://www.thecurrentdomain.com/somedir/somepath/sec/IF00.html", and my href field may or may not have a subdirectory in it (for ex href="page.html"
vs. href="sub/page.html"
), so I cannot always just remove every thing before the last slash.
我想我可以使用字符串操作来做到这一点,但它变得棘手,因为我的本地页面实际上可能是“http://www.thecurrentdomain.com/somedir/somepath/sec/IF00.html”,而我的 href 字段可能或者可能没有子目录(对于 ex href="page.html"
vs. href="sub/page.html"
),所以我不能总是在最后一个斜杠之前删除所有内容。
You may wonder why I am requesting this, and it is because it will just make the page a lot cleaner. If it is not possible to get just the short href (as put in the anchor <a>
tag), then I could probably just insert an extra field into the tag, like link="sec/IF00.html"
, but again, that would be a little messier.
您可能想知道我为什么要这样做,因为它只会使页面更干净。如果不可能只获得短href(如放置在锚<a>
标记中),那么我可能只需在标记中插入一个额外的字段,例如link="sec/IF00.html"
,但同样,那会有点混乱。
回答by aorcsik
The below code gets the full path, where the anchor points:
下面的代码获取完整路径,其中锚点:
document.getElementById("aaa").href; // http://example.com/sec/IF00.html
while the one below gets the value of the href
attribute:
而下面的获取href
属性的值:
document.getElementById("aaa").getAttribute("href"); // sec/IF00.html
回答by Zange-chan
document.getElementById("link").getAttribute("href");
If you have more than one <a>
tag, for example:
document.getElementById("link").getAttribute("href");
如果您有多个<a>
标签,例如:
<ul>
<li>
<a href="1"></a>
</li>
<li>
<a href="2"></a>
</li>
<li>
<a href="3"></a>
</li>
</ul>
You can do it like this: document.getElementById("link")[0].getAttribute("href");
to access the first array of <a>
tags, or depends on the condition you make.
您可以这样做:document.getElementById("link")[0].getAttribute("href");
访问第一个<a>
标签数组,或者取决于您创建的条件。
回答by nazifa rashid
This code works for me to get all links of the document
此代码适用于我获取文档的所有链接
var links=document.getElementsByTagName('a'), hrefs = [];
for (var i = 0; i<links.length; i++)
{
hrefs.push(links[i].href);
}
回答by M.Ganji
The href property sets or returns the value of the href attribute of a link.
href 属性设置或返回链接的 href 属性的值。
var hello = domains[i].getElementsByTagName('a')[0].getAttribute('href');
var url="https://www.google.com/";
console.log( url+hello);
回答by ntihin
document.getElementById("aaa").href; //for example: http://example.com/sec/IF00.html
回答by Yash Jain
Sad to see how many people gave such uncomfortable and inconvenient answers. Here is the easiest way to retrieve href:
看到有多少人给出了如此不舒服和不方便的答案,我很难过。这是检索href的最简单方法:
$("a").click(function(e){ var hash = this.hash; alert(hash); });
$("a").click(function(e){ var hash = this.hash; alert(hash); });
All done in just one line of code. This is flexible and doesn't rely on getting elementId like previous answers.
只需一行代码即可完成所有操作。这是灵活的,并且不像以前的答案那样依赖于获取 elementId。