有没有办法做这样的事情:
var my_element= document.createElement("div");
my_element.style={
position:"absolute",
background:"red",
...
}
而不是:
my_element.style.position= "absolute";
my_element.style.background= "red";
或者
var my_properties={
position:"absolute",
background:"red",
...
}
for( var i in my_properties )
my_element.style[i]= my_properties[i];
答案 0 :(得分:0)
使用for...in
var my_element= document.createElement("div");
var elemStyle = my_element.style;
var my_properties={
position:"absolute",
background:"red",
...
}
for(var prop in my_properties) {
elemStyle[prop] = my_properties[prop];
}
或者你可以setAttribute
寻找风格。
var elemstyle = my_element.getAttribute("style");
my_element.setAttribute(
"style", elemstyle+"font-size: 100px; font-style: italic; color:#ff0000;");