IE8 内部 HTML 错误

发布于 2024-09-02 16:23:08 字数 701 浏览 1 评论 0原文

这个innerHTML代码在IE8中不能可靠地工作: (但正在 IE6 IE7 FF Opera Chrome Safari 中工作) (通过不可靠地工作,我的意思是我已将此代码放置在各种元素的 onmouseover 处理程序中,有时当鼠标悬停在给定元素上时它会更改文本,有时则不会 - 没有任何模式 - 无论它是否有效似乎不是完全随机的)

document.getElementById("mydiv").innerHTML="some text";

DOM 方法(删除并重新添加带有更新文本的 div)也没有帮助。

所以我在上面的代码之后立即添加了这个并修复了它:

document.styleSheets[0].addRule("#mydiv:after", "content: ' ';");

使用条件注释我确定了第二行代码仅适用于 IE8

我确信这会节省人们很多时间,因为我已经在这上面浪费了很多时间! 甚至 Jquery.text() 也没有帮助!

我在其他地方读到,innerHTML 将更新 DOM,但可能无法更新屏幕元素。我相信 CSS 规则有效,因为它动态更改 #mydiv 的伪类之后的内容,因此需要更新 mydiv 的内容,从而更新屏幕(innerHTML 无法做到这一点)。

但是,如果有人有更好的解决方案,我很乐意听到 - 谢谢

按要求编辑

This innerHTML code wasn't working reliably in IE8:
(but was working in IE6 IE7 FF Opera Chrome Safari)
(by not working reliably I mean I had placed this code within onmouseover handlers on various elements, sometimes it would change the text when mousing over a given element and sometimes it wouldn't - there was no pattern to this - whether it would work or not seemed to be totally random)

document.getElementById("mydiv").innerHTML="some text";

DOM methods (removing and re-adding the div with updated text) weren't helping either.

So I added this immediately after the above code and it fixed it:

document.styleSheets[0].addRule("#mydiv:after", "content: ' ';");

Using conditional comments I identified this 2nd line of code as for IE8 only

I am positive this will save people a lot of time since I have wasted ages on this!
Even Jquery.text() wasn't helping!

I have read elsewhere that the innerHTML will update the DOM but can fail to update screen elements. I believe the CSS rule works because it changes the content of the after pseudo class of #mydiv dynamically and thus requires the content of mydiv to be updated, thus updating the screen (something innerHTML was failing to do).

However if anyone has a better solution I'd love to hear - Thanks

EDITED AS REQUESTED

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

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

发布评论

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

评论(1

放我走吧 2024-09-09 16:23:08

您可以使用innerText代替innerHTML

You can use innerText in place of innerHTML

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文