How to use JavaScript to dynamically create or add a CSS style sheet

Source: Internet
Author: User

 

1. A simple method, regardless of whether or not, can directly do this:

Document.createstylesheet().css text = 'tag {color: red;' + // This annotation is only available in the current JS and will not be written to 'width: 300px; height: 150px} '+ '. class Name {......} '+' # IDs {......} ';

// Complete the job. I like writing with semicolons, which is better aligned with the starting position of instruction writing, especially when other statements are followed.

 

2. To prevent repeated addition, you can add a style sheet ID and determine the style sheet ID:

If (! Document. stylesheets ['id of the style table to be created, such as theforever ']) {// check whether the ID of the style table to be created exists to prevent repeated addition of VaR Ss = document. createstylesheet (); SS. owningelement. id = 'id of the style sheet to be created, such as theforever '; ss.css text =' tag {display: inline-block; overflow: hidden; '+ // This annotation can only be understood in the current JS, and will not be written to 'text-align: Left; width: 300px; Height: 150px}' + 'in CSS '. class Name {......} '+' # IDs {......} ';}

 

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.