Js adds multiple styles to elements making them browser compatible

  • 2020-03-30 03:48:24
  • OfStack

Js adds a number of styles to the element, the browser is fully compatible with sample writing:


<a href="javascript:;" id="test" style="font-size:25px;background:#080;"> test 3</a>
<script>
var obj=document.getElementById("test");
var oldStyle=obj.style.cssText;
alert(oldStyle);
obj.style.cssText="border:2px red solid;color:#f00;"+oldStyle;
</script>

Js adds a number of styles to the element, the most efficient and convenient is to use the cssText attribute, but it will overwrite the original value of the entire style, to retain the original style value, it is very simple, as in the example above, with a variable to record the original original value of the style, and then do a string splicture.

However, it is important to note that the last style value returned by ie8 and the following browsers, obj.style.cssText, is unsemicolon. Background: # 080. Just want to say, ie as always let a person feel uncomfortable, hehe.

So example is interested in placing oldStyle behind the string concatenation, such splicing pattern string even if the last style value no semicolon, also won't be a problem, each browser styling is consistent, it is a small tips, and no technical content, but easy to ignore or forget, know I have a bad memory, ^_^ under the Mark


Related articles: