简体   繁体   中英

How to apply css before property via JavaScript?

`   input.star:checked ~ label.star:before {
            content: '\f005';
            color: #FD4;
            transition: all .25s;
        }    `

how can I apply this code by javascript ?

If you want add a style with js, you can use the insertRule() of style.sheet, the example code:

 var sheet = (function() { // Create the <style> tag var style = document.createElement("style"); // WebKit hack :( style.appendChild(document.createTextNode("")); // Add the <style> element to the page document.head.appendChild(style); return style.sheet; })(); // for a test sheet.insertRule("body { background-color:red;}", sheet.length); // for your code just replace param // sheet.insertRule("input.star:checked ~ label.star:before {content: '\\f005';color: #FD4;transition: all .25s;}}", sheet.length); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM