[英]Set element.style with an object of style properties
Is there a way to do something like that: 有没有办法做这样的事情:
var my_element= document.createElement("div");
my_element.style={
position:"absolute",
background:"red",
...
}
instead of: 代替:
my_element.style.position= "absolute";
my_element.style.background= "red";
Or 要么
var my_properties={
position:"absolute",
background:"red",
...
}
for( var i in my_properties )
my_element.style[i]= my_properties[i];
Do it using for...in
用
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];
}
Or you can setAttribute
for style. 或者,您可以为样式设置
setAttribute
。
var elemstyle = my_element.getAttribute("style");
my_element.setAttribute(
"style", elemstyle+"font-size: 100px; font-style: italic; color:#ff0000;");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.