2012-01-03 318 views
1

我有一个很奇怪的问题需要解决;该解决方案应该是纯JavaScript(不允许框架)。这里是:“显示在页面上启用的所有样式表”。document.styleSheets [i] .disabled在chrome中不起作用

因为在IE中有多个问题innerHtml,所以我使用chrome,尽管它具有样式表的cssText属性,它完全适合我的目的。

不管怎么说,这是我的榜样

<html> 
    <head> 
     <style MEDIA="screen"> 
      h1 {color:blue; font-size:10pt; background-color:cyan} 
     </style> 
     <style MEDIA="screen" DISABLED="true"> 
      h4 {color:blue; font-size:10pt; background-color:cyan} 
     </style> 
     <style MEDIA="print"> 
      h1 {color:gray; font-size:12pt; font-weight: bold} 
      h2 {font-style:italic} 
     </style> 
     <style DISABLED="true"> 
      h2 {color:green; font-size:12pt} 
     </style> 
     <style> 
      h3 {font-style:italic} 
     </style> 

     <script language="Javascript"> 
      function displayActiveStyles() { 
       var container = document.getElementById('activeStyles');         
       var i; 
       for (i=0; i<document.styleSheets.length; i++) { 
        alert(document.styleSheets[i].disabled); 
       } 
      }     
     </script> 
    </head> 
    <body> 
     <h1>one</h1> 
     <h2>two</h2> 
     <h3>three</h3> 
     <input type="button" value="show" onclick="displayActiveStyles()" > 
     <hr /> 
     <div id="activeStyles"></div> 
    </body> 
</html> 

的问题是,所有5种样式它提醒false这是不正确的。如果任何人有任何想法我犯了一个错误,或者如果它可以在铬合金可行,请让我知道。

+0

您的语言和用户名...有点冲突;) – Blender 2012-01-03 22:28:25

+0

铬有报告的错误http://code.google.com/p/chromium/issues/detail?id=88310。谷歌正在使用铬源来构建他们的谷歌Chrome浏览器 – 2012-01-03 22:35:52

+0

Blender,=)好点,会“废话”而不是胡说八道吗? – Clergyman 2012-01-04 18:39:00

回答

0

我知道你想列出每个样式表的href,对吧?在你的代码中,你正在浏览每个样式表,并提醒它是禁用的属性。由于他们已启用,他们警告“错误”,这是正确的。如果你想获得只有那些启用的href,你可以这样做:

var i; 
for (i = 0; i < document.styleSheets.length; i++) { 
    if (!document.styleSheets[i].disabled) { 
     alert(document.styleSheets[i].href); 
    } 
} 

这里尝试这个,有一个样式表其其href是null。不太确定这是为什么,我快速搜索了一些东西,这可能是因为它是一个<style>标签。您可以添加支票document.styleSheets[i].href along with the check for已禁用。

+0

谢谢,但不,这不是我想要做的。我的任务是显示启用样式表的规则,但这不是重点,我的问题是.disabled总是返回false,但事实并非如此。 – Clergyman 2012-01-04 08:40:14

相关问题