js写入html ie8,javascript

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值