Home Web Front-end JS Tutorial Insert a new style implementation method into the current style sheet_javascript skills

Insert a new style implementation method into the current style sheet_javascript skills

May 16, 2016 pm 05:38 PM
style style

It is rare to insert a new style rule. Today, in order to temporarily solve the page style problem, some styles of many pages need to be updated. These pages all reference a public js. For convenience, directly insert into the style sheet in this public js. New style rules.

Look at the code first:

Copy the code The code is as follows:

/* *
* Add a stylesheet rule to the document (may be better practice, however,
* to dynamically change classes, so style information can be kept in
* genuine styesheets (and avoid adding extra elements to the DOM))
* Note that an array is needed for declarations and rules since ECMAScript does
* not afford a predictable object iteration order and since CSS is
* order-dependent (i.e., it is cascading); those without need of
* cascading rules could build a more accessor-friendly object-based API.
* @param {Array} decls Accepts an array of JSON-encoded declarations
* @example
addStylesheetRules([
['h2', // Also accepts a second argument as an array of arrays instead
['color', 'red'],
['background-color', 'green', true] // 'true' for !important rules
],
['.myClass',
['background-color', 'yellow']
]
]);
*/
function addStylesheetRules (decls) {
var style = document.createElement('style');
document.getElementsByTagName('head')[0].appendChild(style);
if (!window.createPopup) { /* For Safari */
style.appendChild(document.createTextNode(''));
}
var s = document.styleSheets[document.styleSheets .length - 1];
for (var i=0, dl = decls.length; i < dl; i ) {
var j = 1, decl = decls[i], selector = decl[0 ], rulesStr = '';
if (Object.prototype.toString.call(decl[1][0]) === '[object Array]') {
decl = decl[1];
j = 0;
}
for (var rl=decl.length; j < rl; j ) {
var rule = decl[j];
rulesStr = rule[0] ':' rule[1] (rule[2] ? ' !important' : '') ';n';
}
if (s.insertRule) {
s.insertRule(selector '{ ' rulesStr '}', s.cssRules.length);
}
else { /* IE */
s.addRule(selector, rulesStr, -1);
}
}
}

Copy code The code is as follows:

addStylesheetRules( ["div.content", ["color": "#000"], ["border-width","1px"], ["border-style", "solid"]])

After execution, there is an additional style
Copy code The code is as follows:


You know how to call it? It will happen every time you call it. Insert a new style, so it is best to call it once and insert multiple rules
Copy code The code is as follows:

addStylesheetRules(
[selector, [attr, value], …],
[selector, [attr, value], …]
);

Mainly use two methods :
Standard method: stylesheet.insertRule(rule, index)
rule: inserted rule, such as div.content{color:#000}
index : Insertion order, the order will affect the style. Starting from 0
firefox, chrome, opera, safri, and ie also support this method starting from ie9
ie's stylesheet.addRule (selector, styleDef [, positionIndex]);
selector: such as div.content
styleDef: such as color:#000
positionIndex: default -1, inserted at the end
ie, safari, and chrome support this method
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

macOS: How to change the color of desktop widgets macOS: How to change the color of desktop widgets Oct 07, 2023 am 08:17 AM

macOS: How to change the color of desktop widgets

How to modify element.style How to modify element.style Nov 24, 2023 am 11:15 AM

How to modify element.style

Guide to solving misalignment of WordPress web pages Guide to solving misalignment of WordPress web pages Mar 05, 2024 pm 01:12 PM

Guide to solving misalignment of WordPress web pages

How to dynamically modify style in react How to dynamically modify style in react Dec 28, 2022 am 10:44 AM

How to dynamically modify style in react

What are the new features in Vue3 style and how to use them What are the new features in Vue3 style and how to use them May 14, 2023 pm 10:52 PM

What are the new features in Vue3 style and how to use them

CSS web background image design: create various background image styles and effects CSS web background image design: create various background image styles and effects Nov 18, 2023 am 08:38 AM

CSS web background image design: create various background image styles and effects

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Aug 26, 2023 pm 10:58 PM

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it?

Use the :nth-last-child(2) pseudo-class selector to select the style of the second-to-last child element Use the :nth-last-child(2) pseudo-class selector to select the style of the second-to-last child element Nov 20, 2023 am 11:22 AM

Use the :nth-last-child(2) pseudo-class selector to select the style of the second-to-last child element

See all articles