更改HREF后,IE会忘记A-Tag的主机名 [英] IE forgets an A-Tag's hostname after changing HREF

查看:106
本文介绍了更改HREF后,IE会忘记A-Tag的主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置 href -Attribute一个< a ...> - 在项目。在其他一点上,我查看< a> 的DOM-Property调用 hostname ,以便图无论是否是内部链接。

I set the href-Attribute of an <a ...>-Tag dynamically in a project. 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.

基本上,这是会发生的。

Basically, this is what happens.

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

而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;

(也可以这个小提琴: http://jsfiddle.net/RurQT/

(cf. also this fiddle: http://jsfiddle.net/RurQT/ )

当我设置 d.href 到相对路径,我希望 d.hostname 不变 - 所以 log -Div包含相同的主机名两次。这在FF和Chrome中是正确的。

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. This is correct in FF and Chrome.

然而,InternetExplorer 7,8和9都坚持认为第二次主机名为空。

However, InternetExplorer 7, 8 and 9 all insist that the second time, the hostname is empty.

我特别困惑,因为第一个链接一直是相对的!我没有任何< base href> set,btw。

I am especially confused, because the first link has been relative all along! I don't have any <base href> set, btw.

我非常感谢任何建议我可以使InternetExplorer更新hostname - a的属性 -DOM-Element。

I would greatly appreciate any suggestion how I can make InternetExplorer update the "hostname"-Property of the a-DOM-Element.

推荐答案

您将不得不将其隐藏并重新设置:

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

这篇关于更改HREF后,IE会忘记A-Tag的主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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