2009-11-05 6 views
5

Ich muss, was alle CSS-Stile in einer HTML-Datei mit JavaScript verwendet werden.Getting alle CSS in HTML-Datei verwendet

Wenn der obige Code mein HTML ist, muss ich eine JavaScript-Funktion in den Kopf schreiben, die eine Zeichenfolge wie folgt zurückgibt.

body { 
    border: 1px solid silver; 
} 
.mydiv { 
    color: blue; 
} 

Ist es möglich zu tun?

Antwort

7

Für Inline-Stylesheets können Sie den Inhalt aus dem normalen DOM wie mit jedem anderen Elemente erhalten:

document.getElementsByTagName('style')[0].firstChild.data 

Für externen, link ed Sheets es problematischer ist. In modernen Browsern können Sie den Text jeder Regel (einschließlich Inline-, verknüpfter und @ importierter Stylesheets) von der document.styleSheets[].cssRules[].cssText-Eigenschaft abrufen.

Leider implementiert IE diesen DOM Level 2 Style/Standard nicht, sondern verwendet its own subtly different Version der StyleSheet und CSSRule-Schnittstellen. Daher benötigen Sie einen Code mit Sniff-and-Branch, um Regeln im IE neu zu erstellen, und der Text stimmt möglicherweise nicht genau mit dem Original überein. (Insbesondere wird IE ALL-CAPS Ihre Eigenschaftsnamen und Leerzeichen verlieren.)

var css= []; 

for (var sheeti= 0; sheeti<document.styleSheets.length; sheeti++) { 
    var sheet= document.styleSheets[sheeti]; 
    var rules= ('cssRules' in sheet)? sheet.cssRules : sheet.rules; 
    for (var rulei= 0; rulei<rules.length; rulei++) { 
     var rule= rules[rulei]; 
     if ('cssText' in rule) 
      css.push(rule.cssText); 
     else 
      css.push(rule.selectorText+' {\n'+rule.style.cssText+'\n}\n'); 
    } 
} 

return css.join('\n'); 
+1

Haben Sie wirklich die Variable sheeti nennen ?? :) Es ist in der Tat eine Shevi-Variable .. – Faruz

+0

Dank Bobince ... Arbeitete in allen Browsern ... – DonX

+1

Ich bekomme den folgenden Fehler: 'TypeError: Kann Eigenschaft 'Länge' von null ' – starbeamrainbowlabs

0

Hier ist meine Lösung:

function getallcss() { 
    var css = "", //variable to hold all the css that we extract 
     styletags = document.getElementsByTagName("style"); 

    //loop over all the style tags 
    for(var i = 0; i < styletags.length; i++) 
    { 
     css += styletags[i].innerHTML; //extract the css in the current style tag 
    } 

    var currentsheet = false;//initialise a variable to hold a reference to the stylesheet we are currently extracting from 
    //loop over all the external stylesheets 
    for(var i = 0; i < document.styleSheets.lenngth; i++) 
    { 
     currentsheet = document.styleSheets[i]; 
     //loop over all the styling rules in this external stylesheet 
     for(var e = 0; e , currentsheet.cssRules.length; e++) 
     { 
      css += currentsheet.cssRules[e].cssText; //extract all the styling rules 
     } 
    } 

    return css; 
} 

Es ist auf @ bobince Antwort basiert.

Es extrahiert alle CSS aus den Style-Tags und den externen Stylesheets.

2

Hier ist meine Lösung:

var css = []; 
for (var i=0; i<document.styleSheets.length; i++) 
{ 
    var sheet = document.styleSheets[i]; 
    var rules = ('cssRules' in sheet)? sheet.cssRules : sheet.rules; 
    if (rules) 
    { 
     css.push('\n/* Stylesheet : '+(sheet.href||'[inline styles]')+' */'); 
     for (var j=0; j<rules.length; j++) 
     { 
      var rule = rules[j]; 
      if ('cssText' in rule) 
       css.push(rule.cssText); 
      else 
       css.push(rule.selectorText+' {\n'+rule.style.cssText+'\n}\n'); 
     } 
    } 
} 
var cssInline = css.join('\n')+'\n'; 

Am Ende cssInline ist eine textuelle Liste aller steelsheets der Seite und deren Inhalt.

Beispiel:

/* Stylesheet : http://example.com/cache/css/javascript.css */ 
.javascript .de1, .javascript .de2 { -webkit-user-select: text; padding: 0px 5px; vertical-align: top; color: rgb(0, 0, 0); border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); margin: 0px 0px 0px -7px; position: relative; background: rgb(255, 255, 255); } 
.javascript { color: rgb(172, 172, 172); } 
.javascript .imp { font-weight: bold; color: red; } 

/* Stylesheet : http://example.com/i/main_master.css */ 
html { } 
body { color: rgb(24, 24, 24); font-family: 'segoe ui', 'trebuchet MS', 'Lucida Sans Unicode', 'Lucida Sans', sans-serif; font-size: 1em; line-height: 1.5em; margin: 0px; padding: 0px; background: url(http://pastebin.com/i/bg.jpg); } 
a { color: rgb(204, 0, 51); text-decoration: none; } 
a:hover { color: rgb(153, 153, 153); text-decoration: none; } 
.icon24 { height: 24px; vertical-align: middle; width: 24px; margin: 0px 4px 0px 10px; } 
#header { border-radius: 0px 0px 6px 6px; color: rgb(255, 255, 255); background-color: rgb(2, 56, 89); } 
#super_frame { min-width: 1100px; width: 1200px; margin: 0px auto; } 
#monster_frame { -webkit-box-shadow: rgb(204, 204, 204) 0px 0px 10px 5px; box-shadow: rgb(204, 204, 204) 0px 0px 10px 5px; border-radius: 5px; border: 1px solid rgb(204, 204, 204); margin: 0px; background-color: rgb(255, 255, 255); } 
#header a { color: rgb(255, 255, 255); } 
#menu_2 { height: 290px; } 

/* Stylesheet : [inline styles] */ 
.hidden { display: none; }