JavaScript Dynamic Insert CSS method _javascript tips

Source: Internet
Author: User

When you write components, you sometimes want to encapsulate some of the CSS styles associated with component features in JS, which is more cohesive and convenient to change. JS Dynamic Insert CSS Two steps: Create 1, a Style object
2. Add styles using stylesheet Insertrule or Addrule methods

First, view the style sheet

First look at the document.stylesheets, open a page at random

The first three are CSS files introduced through the link tag, and the fourth is a CSS that is inline with the style tag. Has the following properties

Each cssrule has the following properties

One of the Csstext is written in the style of the source code.

Second, dynamic insert CSS
first, you need to create a style object that returns its Stylesheet object


 * * Create a style that returns its Stylesheet object
 * Note: Use Style.stylesheet in Ie6/7/8, other browsers style.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 function Addcssrule as follows

*
 * Dynamic add CSS style
 * @param selector {string} selector
 * @param rules {string} CSS style Rule
 * @param index {number} insert The rules of the position, the following rules will cover the front of the
 * *
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, while IE low version supports addrule.
The complete code is as follows

*
 * Dynamic add CSS style
 * @param selector {string} selector
 * @param rules {string} CSS style Rule
 * @param index {number} insert The rule position, the back rule will overwrite the front
/var addcssrule = function () {
 ///Create a style, return its Stylesheet object
 //Note: Use Styl in IE6/7/8 E.stylesheet, other browsers style.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 Stylesheet Object
 var sheet = createstylesheet ();
 
 Returns the interface functions 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 only the mobile side or modern browser support, you can remove the low version of IE to judge the code

*
 * Dynamic add CSS style
 * @param selector {string} selector
 * @param rules {string} CSS style Rule
 * @param index {number} insert The position of the rule, the back of the rule will overwrite the front, the default after inserting
/var addcssrule = function () {
 ///Create a style, return its Stylesheet object
 function cre Atestylesheet () {
  var style = document.createelement (' style ');
  Style.type = ' text/css ';
  Document.head.appendChild (style);
  return style.sheet;
 }
 
 Create Stylesheet Object
 var sheet = createstylesheet ();
 
 Returns the interface functions return function
 (selector, rules, index) {
  index = index | | 0;
  Sheet.insertrule (selector + "{" + Rules + "}", index);
 }
();

The above is the JavaScript dynamic inserts the CSS method, hoped that has the help to everybody's study.

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.