IE8、7、6动态添加样式时,CSS hack的BUG
来源:互联网 发布:硬盘修复软件 编辑:程序博客网 时间:2024/06/06 09:41
问题描述
下面这段CSS代码通过JS动态添加,结果会怎样呢?
.box { background: red; /* normal browsers */ *background: blue; /* IE 6 and 7 */ _background: green; /* IE6 */}
通过以下代码添加到页面中
var node = document.createElement('style');node.type = 'text/css';if (node.styleSheet) { // for w3cnode.styleSheet.cssText = style;} else { // for ienode.appendChild(document.createTextNode(style));}document.getElementsByTagName('head')[0].appendChild(node);
从代码来看,>=IE8和现代浏览器的.box的颜色是红色的,IE7是蓝色的,IE6是绿色的,那么实际呢?
IE6:
IE7:
IE8:
Chrome:
怎么回事???
从上面的截图可以看到,只有Chrome和IE6是正常的,IE7和IE8表现有问题。
IE7的表现为使用了IE6 hack(_ hack)
IE8的表现为使用了IE7 hack(* hack)
解决方法
直接上代码:
var node = document.createElement('style');node.type = 'text/css';document.getElementsByTagName('head')[0].appendChild(node);if (node.styleSheet) { // for w3cnode.styleSheet.cssText = style;} else { // for ienode.appendChild(document.createTextNode(style));}
其实没什么变化,就是移动了appendChild那一句代码的位置。
由原来的先添加CSS样式,然后再添加到head上,变成了先添加到head上面,再更新CSS样式。
再次刷新IE7、8,现在的表现正常了。
贴上测试代码:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style>.box{width: 100px;height: 100px;}</style></head><body><div class="box"></div><script>var node = document.createElement('style'),style = '.box {background: red;*background: blue;_background: green;}';node.type = 'text/css';document.getElementsByTagName('head')[0].appendChild(node);if (node.styleSheet) { // for w3cnode.styleSheet.cssText = style;} else { // for ienode.appendChild(document.createTextNode(style));}</script></body></html>
参考文章:https://www.phpied.com/the-star-hack-in-ie8-and-dynamic-stylesheets/
阅读全文
0 0
- IE8、7、6动态添加样式时,CSS hack的BUG
- IE8的css hack
- IE8的css hack
- IE8的css hack
- IE8的css hack
- 针对firefox ie6 ie7 ie8的css样式hack
- 针对firefox ie6 ie7 ie8的css样式hack
- 针对firefox ie6 ie7 ie8的css样式hack
- IE8的css hack /9
- 针对ie8的css hack
- ie8 css hack 只适用ie8的css写法
- IE8的css hack(浏览器识别码)
- IE8的CSS hack(转)
- IE8的css hack \9 使用说明
- IE8的css hack \9 使用说明
- 仅针对IE8的css hack
- IE8 css hack
- IE8 CSS hack
- rotate-image
- AngularJS 学习笔记(4)-过滤器,$location
- ArcEngine创建要素类_线图层
- 项目1-C++语言中函数参数传递的三种方式
- 算法提高 ADV-135 三角形面积
- IE8、7、6动态添加样式时,CSS hack的BUG
- MySQL单表查询多表查询
- 对于loader constraint violation问题的解决方案
- luogu1262 间谍网络(tarjan缩点)
- Pandas 使用教程 3
- spring boot velocity自定义指令 java
- linux下mysql安装 主从复制和读写分离配置
- repo init报错Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
- hehheda