Chrome中关于document. stylesheet的怪异行为
Weird behaviour in Chrome regarding document.styleSheets
我有这个代码…
function disableSheets(){
console.log(document.styleSheets.length);
var c = document.styleSheets.length;
for(var i=0;i<c;i++){
console.log(document.styleSheets[i]);
if(typeof document.styleSheets[i]!=='undefined' && document.styleSheets[i].href.indexOf('stylezone')!=-1){
document.styleSheets[i].disabled=true;
}
}
console.log(document.styleSheets.length);
}
当我在Firefox/Firebug中运行它时,它显示:
3
StyleSheet
StyleSheet
StyleSheet
3
当我在Chrome/Developer Tools中运行它时,它说:
3
CSSStyleSheet
CSSStyleSheet
undefined
1
我的问题是:
- 如果第三个样式表没有定义,为什么会说有3个样式表?
- 我是如何在循环结束时丢失2个样式表的?
- 第三张纸怎么了?
在我的HTML顶部,我有3个<link>
s,链接3个样式表,然后紧接着我调用disableSheets()
。
显然,样式表从document.styleSheets
对象(Chrome)中删除,当禁用时。
你的代码实际上读取了第一个和第三个样式表:
概述:
- 3个样式表:A, B, C
-
var c = document.styleSheets.length;
循环 - , i = 0
-
i=0
, SelectstyleSheet[i]
=StyleSheet A
2个样式表:B, C 循环 - , i = 1
-
i=1
, SelectstyleSheet[i]
=StyleSheet C
1 .样式表左:B 循环 - ,我= 2
-
i=2
, SelectstyleSheet[i]
=undefined
- Loop
i<3
=break
-
Console.log(document.styleSheets.length)
=1
(样式表B)
让你的代码工作,反向循环:
for(var i=c-1; i>=0; i--){
注意:初始化i
在 c-1
!JavaScript类数组对象的索引是从零开始的
在考虑了一段时间后,我找到了一个解决方案:
只是分配文件。
在stylesheet上设置disable=true
将从document. stylesheet中删除它
这是一个错误在Chrome, Safari
http://code.google.com/p/chromium/issues/detail?id=88310相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何用更合适的内容替换document.write
- document.styleSheets不返回任何内容
- jQuery document.ready not working
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- reactjs this.refs vs document.getElementById
- 如何通过adf中的document.getElementById获取inputText字段值
- window.opener.document在ie中不起作用
- contentWindow.document.body is null
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- document.getElementById(“st”).click();不起作用
- $(document).height和$(window).heaght都返回相同的值-使用的是正确的doctype
- jQuery document.ready停止代码
- document.getElementById并使用id名称
- 使用jQuery 1.8.1准备好多个$(document).是否有任何开销
- document.getElementById在js中不起作用
- document.getElementById.style.backgroundImage not working
- 停止对document.ready函数的重定向/刷新
- Chrome中关于document. stylesheet的怪异行为