简体   繁体   English

在更改HREF后,IE忘记了A-Tag的主机名

[英]IE forgets an A-Tag's hostname after changing HREF

I set the href -Attribute of an <a ...> -Tag dynamically in a project. 我在项目中动态设置了<a ...>href <a ...> At some other point, I check the <a> 's DOM-Property called hostname , in order to figure out whether or not it's an internal link. 在其他方面,我检查<a>的DOM-Property,称为hostname ,以确定它是否是内部链接。

Basically, this is what happens. 基本上,这就是发生的事情。

<!--HTML-->
<a id="my" href="/my/first/link">MyLink</a>
<div id="log"></div>

And JS is: JS是:

// js
var a = document.getElementById( 'my' ),
    log = document.getElementById( 'log' );

log.innerHTML += a.hostname + '<br/>';

a.setAttribute('href',"/my/other/link");
log.innerHTML += a.hostname;

(cf. also this fiddle: http://jsfiddle.net/RurQT/ ) (参见这个小提琴: http//jsfiddle.net/RurQT/

As I set d.href to a relative path, I expect d.hostname to be unchanged - so the log -Div contains the same Hostname twice. 当我将d.href设置为相对路径时,我希望d.hostname保持不变 - 因此log -Div包含两次相同的主机名。 This is correct in FF and Chrome. 这在FF和Chrome中是正确的。

However, InternetExplorer 7, 8 and 9 all insist that the second time, the hostname is empty. 但是,InternetExplorer 7,8和9都坚持第二次,主机名为空。

I am especially confused, because the first link has been relative all along! 我特别困惑,因为第一个链接一直是相对的! I don't have any <base href> set, btw. 我没有任何<base href>设置,顺便说一下。

I would greatly appreciate any suggestion how I can make InternetExplorer update the "hostname"-Property of the a -DOM-Element. 我非常感谢任何建议如何让InternetExplorer更新a -DOM-Element的“hostname”属性。

You'll have to stash and re-set it it seems: 你必须隐藏并重新设置它似乎:

var d = document.getElementById( 'my' ),
var h = d.hostname
d.setAttribute('href',"/my/other/link");
d.hostname = h

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

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