2016-11-30 74 views

回答

1

假设你要定义的属性来动态设置,你可以用下面的办法:

var example = 'background'; 
element.style[example] = 'black'; 

More on how to access object properties on MDN.

+0

谢谢你这个快速的答案,不知道这是可能的JS。我尝试过'style {example}'和'style(example)',但我需要'[]' –

1

您可以使用括号记号

var example = "background"; 
element.style[example] = "#fff"; 
+0

谢谢!这是一个快速的答案 –