Internet Explorer 不支持 DOM 元素的 HREF 属性?
下面的代码在 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将其更改为:
Try changing it to: