“Inline -Styling JavaScript” Code-Antworten

Fügen Sie Style JavaScript hinzu

// Bad:
element.setAttribute("style", "background-color: red;");
// Good:
element.style.backgroundColor = "red";
Dizzy Dog

Inline -Styling JavaScript

// Change the background color to red
document.body.style.backgroundColor = "red"; 
Inquisitive Iguana

Konvertieren Sie alle Stile in den Inline -Stil JavaScript

var el = document.querySelector("#answer-25097808 > div > div.answercell.post-layout--right > div.s-prose.js-post-body > pre"); // change yourId to id of your element, or you can write “body” and it will convert all document
var els = el.getElementsByTagName("*");

for(var i = -1, l = els.length; ++i < l;){
    el = els[i]
    s = getComputedStyle(el)
    for (let styleKey in el.style) {
        for (let computedStyleKey in s) {
            let computedStyleKeyCamelCase = computedStyleKey.replace(/\-([a-z])/g, v => v[1].toUpperCase());
            if ((typeof el.style[styleKey] != "function") && (styleKey != 'cssText')){
                if(styleKey == computedStyleKeyCamelCase) {
                    el.style[styleKey] = s[computedStyleKey];
                }
            }
        }
    }
}
Crazy Capybara

Ähnliche Antworten wie “Inline -Styling JavaScript”

Fragen ähnlich wie “Inline -Styling JavaScript”

Weitere verwandte Antworten zu “Inline -Styling JavaScript” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen