可利用with(**.style){}
,with(){}的意思是{}里面可以省略掉with()里()的对象。
下面给出一个绘制关闭按钮的例子。
var btn = document.createElement("div");
btn.innerHTML = "×";
with (btn.style) {
//写各种样式
position = "absolute";
top = "-3px";
right = "-3px";
width = "16px";
height = "16px";
borderRadius = "8px";
backgroundColor = "#e95420";
textAlign = "center";
color = "#fff";
lineHeight = "16px";
fontSize = "10px";
fontFamily = "SimHei";
cursor = "pointer";
}
参考来源:https://zhidao.baidu.com/question/134214906.html