简体   繁体   English

如何使用CSS属性遍历对象并分配给样式属性?

[英]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? 如何在下面的for循环中将对象值分配给按钮样式属性?

(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 更多: http : //www.javascripttoolbox.com/bestpractices/#squarebracket

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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