简体   繁体   English

innerHTML错误IE8

[英]innerHTML bug IE8

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) 此innerHTML代码在IE8中无法可靠运行:(但在IE6 IE7 FF Opera Chrome Safari中运行)(由于无法可靠运行,我的意思是我已将此代码放置在onmouseover处理程序中的各种元素上,有时它会在鼠标移动时更改文本在给定的元素上,有时不会-没有模式-它是否起作用似乎完全是随机的)

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

DOM methods (removing and re-adding the div with updated text) weren't helping either. DOM方法(用更新后的文本删除并重新添加div)也无济于事。

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 使用条件注释,我确定了第二行代码仅适用于IE8

I am positive this will save people a lot of time since I have wasted ages on this! 我很肯定,因为我为此浪费了很多时间,这将为人们节省很多时间! Even Jquery.text() wasn't helping! 甚至Jquery.text()Jquery.text()

I have read elsewhere that the innerHTML will update the DOM but can fail to update screen elements. 我在其他地方读过,innerHTML将更新DOM,但可能无法更新屏幕元素。 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). 我认为CSS规则之所以有效,是因为它动态地更改了#mydiv伪类之后的内容,因此需要更新mydiv的内容,从而更新了屏幕(innerHTML无法执行此操作)。

However if anyone has a better solution I'd love to hear - Thanks 但是,如果有人有更好的解决方案,我很想听听-谢谢

EDITED AS REQUESTED 根据要求编辑

您可以使用innerText代替innerHTML

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

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