我构建了一个类似于的 javascript/css 依赖管理器http://code.google.com/p/jingo/ http://code.google.com/p/jingo/.
它允许我做这样的事情:
Loader.DependsOn(['/scripts/jqueryui.js', '/scripts/jquery.js'])
.Css('/css/jquery.css')
.Execute(function() { //Insert script here});
Loader 将在执行之前动态加载脚本和 css(如果尚未加载)。
一切正常,除了 IE 之外。通过将链接附加到文档的头部来加载样式表。然而,在执行此操作之前,加载程序会查看请求的 css 文件本身是否依赖于另一个模块。如果是,它会找出应该插入 CSS 文件顺序的位置。在除 IE 之外的所有浏览器中,如果我将其插入列表的开头,则后面的任何其他样式表都将覆盖它的样式(预期行为)。然而,在 IE 中,尽管它被插入到开头,但 IE 会将其视为结尾。
有没有办法强制 IE 重新计算样式?
更新了测试用例:
创建两个样式表,sheet1.css,sheet2.css
sheet1.css
.testdiv
{
width:200px;
height:200px;
background-color:#c7c7c7;
}
sheet2.css
.testdiv
{
width:400px;
height:400px;
}
markup:
<html>
<head>
<link href="style1.css" rel="stylesheet" "type="text/css" />
</head>
<body>
<div class="testdiv">
</div>
</body>
</html>
<script type="text/javascript" language="javascript>
setTimeout(function() {
var link = document.createElement('link');
link.href = 'sheet2.css';
link.rel = 'stylesheet';
link.type = 'text/css';
var head = document.head || document.getElementsByTagName('head')[0];
head.insertBefore(link, head.children[0]);
setTimeout(function () {
//suggestion from Simon West
document.body.className = document.body.className;
}, 3000);
}, 3000);
</script>
应该发生什么:
灰色框 200 像素 x 200 像素。 3秒后,它仍然存在。不用找了。
IE8 上会发生什么
灰色框 200 像素 x 200 像素。 3 秒后,它会增长到 400 像素 x 400 像素。
Safari(Windows)上会发生什么 -
灰色框 200 像素 x 200 像素。 3秒后,它仍然存在。不用找了。
无论有没有 @Simon West 的建议,都会发生这种情况。