简体   繁体   English

从锚点 (a) 标签获取本地 href 值

[英]Get local href value from anchor (a) tag

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.我有一个具有本地 href 值的锚标记,以及一个使用 href 值但将其定向到与通常情况略有不同的位置的 JavaScript 函数。 The tag looks like标签看起来像

<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".我希望item.href只会返回一个短字符串“sec/IF00.html”,而是返回完整的 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?有没有办法可以只提取锚<a>标签中的短href? Or do I lose that by natural HTML behavior?或者我会因为自然的 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 字段可能或者可能没有子目录(例如href="page.html"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" ,但同样,那会有点乱。

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

document.getElementById("link").getAttribute("href"); If you have more than one <a> tag, for example:如果您有多个<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");你可以这样做: document.getElementById("link")[0].getAttribute("href"); to access the first array of <a> tags, or depends on the condition you make.访问<a>标记的第一个数组,或者取决于您创建的条件。

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);
}

In my case I had a href with a # and target.href was returning me the complete url.就我而言,我有一个带有 # 的 href,而 target.href 正在返回完整的 url。 Target.hash did the work for me. Target.hash 为我完成了这项工作。

$(".test a").on('click', function(e) {
    console.log(e.target.href); // logs https://www.test.com/#test
    console.log(e.target.hash); // logs #test
  });

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);
document.getElementById("aaa").href; //for example: http://example.com/sec/IF00.html

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); }); 

All done in just one line of code. 全部只用一行代码完成。 This is flexible and doesn't rely on getting elementId like previous answers. 这很灵活,不依赖于像以前的答案那样获取elementId。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM