JavaScript _ javascript tips

Source: Internet
Author: User
This article mainly introduces how to insert CSS dynamically in JavaScript. when JavaScript is inserted dynamically in two steps, sometimes we want to encapsulate CSS styles related to some component features in JS, this is more cohesive and convenient to change. JS dynamic insert CSS two steps: create 1. a style object
2. use the insertRule or addRule method of stylesheet to add a style.

1. view the style sheet

Take a look at document. styleSheets and open a page at will.

The first three are CSS files imported through the link tag, and the fourth is CSS files inline in the page through the style tag. Has the following attributes:

Each cssRule has the following attributes:

CssText is the source code written in style.

II. insert CSS dynamically
First, create a style object and return its stylesheet object.

/** Create a style and return its stylesheet object * Note: style is used in IE6/7/8. stylesheet, style in other browsers. sheet */function createStyleSheet () {var head = document. head | document. getElementsByTagName ('head') [0]; var style = document. createElement ('style'); style. type = 'text/css '; head. appendChild (style); return style. sheet | style. styleSheet ;}

Add the addCssRule function as follows:

/** Dynamically add CSS styles * @ param selector {string} selector * @ param rules {string} CSS style rule * @ param index {number} insert the location of the rule, the backward rule will overwrite the previous */function addCssRule (selector, rules, index) {index = index | 0; if (sheet. insertRule) {sheet. insertRule (selector + "{" + rules + "}", index);} else if (sheet. addRule) {sheet. addRule (selector, rules, index );}}

Note that standard browsers support insertRule and earlier IE versions support addRule.
The complete code is as follows:

/** Dynamically add CSS styles * @ param selector {string} selector * @ param rules {string} CSS style rule * @ param index {number} insert the location of the rule, the backward rule overwrites the previous */var addCssRule = function () {// creates a style and returns its stylesheet object. // Note: style is used in IE6/7/8. stylesheet, style in other browsers. sheet function createStyleSheet () {var head = document. head | document. getElementsByTagName ('head') [0]; var style = document. createElement ('style'); style. type = 'text/css '; head. appendChild (style); return style. sheet | style. styleSheet;} // create the stylesheet object var sheet = createStyleSheet (); // return interface function return function (selector, rules, index) {index = index | 0; if (sheet. insertRule) {sheet. insertRule (selector + "{" + rules + "}", index);} else if (sheet. addRule) {sheet. addRule (selector, rules, index );}}}();


If you only support mobile terminals or modern browsers, you can remove the code judged by earlier IE versions.

/** Dynamically add CSS styles * @ param selector {string} selector * @ param rules {string} CSS style rule * @ param index {number} insert the location of the rule, the backward rule will overwrite the previous one. by default, */var addCssRule = function () {// creates a style and returns its stylesheet object function createStyleSheet () {var style = document. createElement ('style'); style. type = 'text/css '; document. head. appendChild (style); return style. sheet;} // create the stylesheet object var sheet = createStyleSheet (); // return the interface function return function (selector, rules, index) {index = index | 0; sheet. insertRule (selector + "{" + rules + "}", index );}}();

The above is the method for dynamically inserting CSS in JavaScript. I hope it will be helpful for your learning.

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.