Internet Explorer 不支持 DOM 元素的 HREF 属性?

发布于 2024-11-26 03:52:24 字数 406 浏览 2 评论 0原文

下面的代码在 Chrome 和 FireFox 中运行良好,但当我尝试在 IE8 中设置 href 属性时出现错误。错误是“对象不支持此属性或方法”。代码如下:

var elem = document.getElementById("linkToGoogle")
if (elem)
{
    elem.href = "https://www.google.com/"
    elem.target = "_blank";
}

我进行了一些网络搜索,发现 IE8 的“兼容性视图”似乎存在一些问题。我不记得曾经在我的 IE8 实例中设置过它,而且我当然没有使用 Meta 标记来请求它。即便如此,我还是需要兼容。有没有快速解决这个问题的方法?请注意,我还尝试替换元素的 externalHTML 内容,但得到了相同的错误消息。

The code below works fine in Chrome and FireFox, but gives an error in when I try to set the href property in IE8. The error is "Object doesn't support this property or method". Here's the code:

var elem = document.getElementById("linkToGoogle")
if (elem)
{
    elem.href = "https://www.google.com/"
    elem.target = "_blank";
}

I did some Web searches and there seems to be some issues with IE8 having something called "Compatibility View". I don't remember ever setting that in my IE8 instance and I am certainly not using the Meta tag to request it. Even so, I still need to be compatible with it. Is there a quick solution to this? Note, I also tried replacing the element's outerHTML content instead and that got the same error message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2024-12-03 03:52:24

尝试将其更改为:

var elem = document.getElementById("linkToGoogle")
if (elem)
{
    elem.setAttribute('href',"https://www.google.com/");
    elem.setAttribute('target ',"_blank");
}

Try changing it to:

var elem = document.getElementById("linkToGoogle")
if (elem)
{
    elem.setAttribute('href',"https://www.google.com/");
    elem.setAttribute('target ',"_blank");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文