简体   繁体   中英

How to iterate over object with css properties and assign to style attribute?

this is probably ver simple but I cannot figure it out.

how can I assign the objects values to the buttons style atribute in the for loop below?

(function(){
    var parent = {
        color: "orange",
        width: "50px",
        height: "50px"
    }, 
        child = Object.create(parent),
        button = document.querySelector("button");

    button.onclick = function(){
        for (var test in child) {
            button.style.test = // ??
        }
    }

})();

thanks in advance

For such cases there is a square bracket notation :

button.style[test] = child[test];

MORE: http://www.javascripttoolbox.com/bestpractices/#squarebracket

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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