“en línea de estilo JavaScript” Código de respuesta

Agregar estilo JavaScript

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

en línea de estilo JavaScript

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

Convierta todos los estilos a JavaScript de estilo en línea

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

Respuestas similares a “en línea de estilo JavaScript”

Preguntas similares a “en línea de estilo JavaScript”

Más respuestas relacionadas con “en línea de estilo JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código