开发者

Save property to variable and append it to DOM element

开发者 https://www.devze.com 2023-03-09 10:58 出处:网络
The code bellow does not work, obviouslyobjects can\'t be saved to variables in that manner! function styleElement(aNode){

The code bellow does not work, obviously objects can't be saved to variables in that manner!

function styleElement(aNode){
    var cssProperty; var cssValue;
    for(var c=0; c<2; c++){     
        cssProperty = c ? backgroundColor : color;
        cssValue = c ? 'blue' : '#fff';
        aNode.style.cssProperty = cssValue;
    }

Would somebody show me the right way? 10x and B开发者_运维问答R, Stephan


You need to use bracket notation and strings:

function styleElement(aNode){
    var cssProperty; var cssValue;
    for(var c=0; c<2; c++){     
        cssProperty = c ? "backgroundColor" : "color";
        cssValue = c ? 'blue' : '#fff';
        aNode.style[cssProperty] = cssValue;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消