如何解决将属性保存到变量并将其附加到DOM元素
| 下面的代码不起作用,显然不能以这种方式将对象保存到变量中!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;
}
有人会告诉我正确的方法吗?
10x和BR,斯蒂芬
解决方法
您需要使用括号符号和字符串:
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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。