开发者

How to make multiple .setAttribute on a single element

开发者 https://www.devze.com 2023-01-27 03:42 出处:网络
m开发者_如何学Goy code is elem.setAttribute( \'style\',\'background-color:\' + l_DivBgcolor); and i want also to add a border color same to the color of the bgcolor..

m开发者_如何学Goy code is

elem.setAttribute( 'style','background-color:' + l_DivBgcolor);

and i want also to add a border color same to the color of the bgcolor.. my problem is how do i add another style to the current style after setting a bgcolor?

i trien putting

elem.setAttribute( 'style','border-color:' + l_DivBgcolor  );

after the first setAttribute but it removes the bgcolor ang set the border color only..


Don't use an attribute for this. Attributes are incorrectly implemented in IE and are almost never what you need. Also, assigning to the style attribute will wipe out existing inline styles, including those set by script. Use the universally-supported style property instead:

elem.style.backgroundColor = l_DivBgcolor;
elem.style.borderColor = l_DivBgcolor;


Can be done with:

elem.setAttribute( 'style','border-color:' + l_DivBgcolor + '; background-color:' + l_DivBgcolor + ';');

Or you could try:

elem.style.display.borderColor = lDivBgcolor;
elem.style.display.backgroundColor = lDivBgcolor;


You can add to (+=) or replace (=) inline style rules with style.cssText.

When appending, any properties that exist in the original style are replaced with the new values. New properties are added, and any in the original but not in the replacement are left as they are.

elem.style.cssText+=';background-color:'+ l_DivBgcolor+';border-color:'+l_DivBgcolor;


Just concatenate them :

elem.setAttribute( 'style','background-color:' + l_DivBgcolor + ';border-color:' + l_DivBgcolor + ';');


Use This

elem.setAttribute( 'style','background-color:' + l_DivBgcolor+ ' border-color:' + l_DivBgcolor);


Variable and hard code values:

element.setAttribute( 'style','top:' + t + '; left:' + l + '; width:' + w + '; height:' + h + ';');
element.setAttribute( 'style','top: 0; left: 0 ; right: 0; bottom: 0; opacity: 0.7;');


you can use the Object and set multiAttribute! for Example if use a tag and

let attGroup={
id:'demo',
class:'style',
href:'#'
};

for(let key in attGroup){
 elem.setAttribute(`${key}`,`${attGroup[key]}`);
}
<a id="demo" class="style" href="#">Link</a>


Since you are changing the style attribute and not the style properties of the element directly you can concatenate the styles in a string

var atValue = 'border-color:' + l_DivBgcolor + ';';
atValue += 'background-color:' + l_DivBgcolor + ';';

elem.setAttribute( 'style', atValue );
0

精彩评论

暂无评论...
验证码 换一张
取 消