如何在单个元素上创建多个.setAttribute

JCm*_*JCm 2 javascript attributes dom

我的代码是

elem.setAttribute( 'style','background-color:' + l_DivBgcolor);
Run Code Online (Sandbox Code Playgroud)

我还想添加一个与bgcolor颜色相同的边框颜色..我的问题是如何在设置bgcolor后为当前样式添加另一种样式?

我trien put

elem.setAttribute( 'style','border-color:' + l_DivBgcolor  );
Run Code Online (Sandbox Code Playgroud)

在第一个setAttribute之后但它删除了bgcolor ang仅设置边框颜色..

Tim*_*own 6

不要为此使用属性.IE中的属性实现不正确,几乎不是您需要的.此外,指定style属性将清除现有的内联样式,包括脚本设置的样式.请改用通用支持的style属性:

elem.style.backgroundColor = l_DivBgcolor;
elem.style.borderColor = l_DivBgcolor;
Run Code Online (Sandbox Code Playgroud)