“JavaScript gaya inline” Kode Jawaban

Tambahkan gaya javascript

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

JavaScript gaya inline

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

Konversi semua gaya menjadi javascript gaya inline

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

Jawaban yang mirip dengan “JavaScript gaya inline”

Pertanyaan yang mirip dengan “JavaScript gaya inline”

Lebih banyak jawaban terkait untuk “JavaScript gaya inline” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya