JS to add a number of styles to the elements of the writing compatible with all browsers

Source: Internet
Author: User

JS adds multiple styles to an element, the quickest and easiest thing to do is to use the Csstext property, but it overrides the value of the entire style, and to retain the original style style value, it's simple, as in the example above, to record the original style value with a variable, and then do a concatenation of the strings.

<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>

But note: IE8 and the following browser Obj.style.cssText returned the last style value is not semicolon, in the form of: Font-size:25px;background: #080. Just want to say, ie as always make people feel uncomfortable, hehe.

In the example, the intent is to place the oldstyle behind the string concatenation, this concatenation of the style string even if the last style value does not have a semicolon, there will be no problem, the browser style application display is consistent, this is a small skill, there is no technical content, but easy to ignore or forget, know that their memory is bad, Mark, ^_^.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.