JavaScript CSS: How to create or add CSS style sheets dynamically with JavaScript

Source: Internet
Author: User
Tags add


1. The simple way, regardless, directly this can be:
Document.createstylesheet (). Csstext = ' label {color:red; ' +
This comment only helps understand in the current JS and is not written in CSS
' width:300px;height:150px} ' +
'. Class name {...} ' +
' #ID们 {...} '
;
Finish the job. I like the semicolon to write, and it's better to align it with the beginning of the instruction, especially when there are other statements behind it.
2. A perfect way to prevent duplicate additions can be achieved by adding a stylesheet ID and judging it:
if (!document.stylesheets[' to create a style sheet ID such as Theforever ']) {//first check to see if the style sheet ID you want to create exists to prevent repeated additions
var ss = Document.createstylesheet ();
Ss.owningElement.id = ' to be established style sheet ID such as Theforever ';
Ss.csstext = ' label {display:inline-block;overflow:hidden; ' +
This comment only helps understand in the current JS and is not written in CSS
' text-align:left;width:300px;height:150px} ' +
'. Class name {...} ' +
' #ID们 {...} '
;
}

This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130102/35506.html

Related Article

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.