繁体   English   中英

document.styleSheets [i] .disabled在chrome中不起作用

[英]document.styleSheets[i].disabled doesn't work in chrome

我有一个很奇怪的问题要解决; 解决方案应为纯JavaScript(不允许使用任何框架)。 在这里是:“显示页面上启用的所有样式表”。

我使用chrome是因为IE中有多个innerHtml问题,尽管它具有样式表的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 ,这是不对的。 如果有人对我在哪里犯了错误有任何想法,或者完全可以在Chrome中执行,请告诉我。

我了解您要列出每个样式表的href,对吗? 在您的代码中,您要遍历每个样式表,并警告其为disabled属性。 由于已启用它们,因此它们会警告“ false”,这是正确的。 如果您只想获取已启用选项的href,则可以执行以下操作:

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

在这里尝试一下,有一个styleSheet,它的href为null 我不太确定为什么,我在Google上搜索了一些内容,这可能是因为它是<style>标签。 您可以添加对document.styleSheets[i].href along with the check for禁用document.styleSheets[i].href along with the check for

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM