JavaScript dynamically modifies CSS styles

Source: Internet
Author: User

Method One: Change the external CSS file, this is not spoken here.

Method Two: By changing the claaname to change the style, syntax:

Obj.classname = "Style2"; // or Obj.setattribute ("Class", "Style2");

Method Three: Use Obj.style.cssTest, Syntax:

Obj.style.cssText = "Display:block; Color:white; "

Advantages of this method:

You can add several properties like this.

Cons: Will clear the original csstext of obj, Solution: Use additive, Syntax:

Obj.style.cssText + = "display:block; Color:white; "

The above Csstext cumulative method is not valid in IE.

However, you can add a semicolon to the front to fix the problem:

Obj.style.cssText + = "; Display:block; Color:white; "

Note : If the style sheet file is preceded by a div {text-decoration:underline;}, will this be overwritten? No! Because it is not a style property that directly acts on the HTML element.

Method Four: Use Obj.style.backgroundColor, Syntax:

Obj.style.backgroundcolor= "BLACK";

Note: BackgroundColor can be replaced by other properties, here are just examples, such as color,position.

One last point of knowledge: CSS Sprite,

is to have a big picture, by controlling the background-position to realize a part of the large image. Can be used to make animations. This large figure is as Background-image:.

The advantage is to reduce the HTTP request, the disadvantage is to locate trouble. Maintenance trouble.

For specific usage, please visit: 24181675

JavaScript dynamically modifies CSS styles

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.