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”。有没有办法可以拉出锚点标签中的短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”与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(如锚标签中所示),那么我可能只是在标签中插入一个额外的字段,如link =“sec / IF00.html”,但同样,这会有点麻烦。
5 个解决方案
#1
126
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
#2
4
document.getElementById("link").getAttribute("href");
If you have more than one <a>
tag, for example:
的document.getElementById( “链接”)的getAttribute(的 “href”);如果您有多个标记,例如:
<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”);访问第一个标签数组,或取决于您所做的条件。
#3
0
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);
#4
-1
document.getElementById("aaa").href; //for example: http://example.com/sec/IF00.html
#5
-7
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。
#1
126
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
#2
4
document.getElementById("link").getAttribute("href");
If you have more than one <a>
tag, for example:
的document.getElementById( “链接”)的getAttribute(的 “href”);如果您有多个标记,例如:
<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”);访问第一个标签数组,或取决于您所做的条件。
#3
0
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);
#4
-1
document.getElementById("aaa").href; //for example: http://example.com/sec/IF00.html
#5
-7
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。