search
HomeWeb Front-endJS TutorialInsert a new style implementation method into the current style sheet_javascript skills

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 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 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
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
macOS:如何更改桌面小部件的颜色macOS:如何更改桌面小部件的颜色Oct 07, 2023 am 08:17 AM

在macOSSonoma中,小部件不必隐藏在屏幕外,也不必像在以前版本的Apple的macOS中那样在通知中心面板中被遗忘。相反,它们可以直接放置在Mac的桌面上–它们也是交互式的。不使用时,macOS桌面小部件会采用单色样式淡入背景,从而减少干扰,并允许您专注于活动应用程序或窗口中手头的任务。但是,当您单击桌面时,它们将恢复为全彩色。如果您更喜欢单调的外观,并且希望在桌面上保留这一方面的统一性,那么有一种方法可以使其永久化。以下步骤演示了它是如何完成的。打开“系统设置”应用

element.style怎么修改element.style怎么修改Nov 24, 2023 am 11:15 AM

element.style修改元素的方法:1、修改元素的背景颜色;2、修改元素的字体大小;3、修改元素的边框样式;4、修改元素的字体样式;5、修改元素的水平对齐方式。详细介绍:1、修改元素的背景颜色,其语法为“document.getElementById("myElement").style.backgroundColor = "red";”;2、修改元素的字体大小等等。

react 怎么动态修改stylereact 怎么动态修改styleDec 28, 2022 am 10:44 AM

react动态修改style的方法:1、在需要修改样式的元素上添加ref,其语法如“<div className='scroll-title clear-fix' ref={ this.manage }>”;2、通过动态控制状态的变化修改元素的样式;3、通过在DOM中使用JS代码实现不同DOM的展示与隐藏转换。

WordPress网页错位现象解决攻略WordPress网页错位现象解决攻略Mar 05, 2024 pm 01:12 PM

WordPress网页错位现象解决攻略在WordPress网站开发中,有时候我们会遇到网页元素错位的情况,这可能是由于不同设备上的屏幕尺寸、浏览器兼容性或者CSS样式设置不当所致。要解决这种错位现象,我们需要仔细分析问题、查找可能的原因,并逐步进行调试和修复。本文将分享一些常见的WordPress网页错位问题以及相应的解决攻略,同时提供具体的代码示例,帮助开

Vue3 style中新增的特性有哪些及怎么用Vue3 style中新增的特性有哪些及怎么用May 14, 2023 pm 10:52 PM

style新特性Vue3.2版本对单文件组件的style样式进行了很多升级,如局部样式、css变量以及样式暴露给模板使用等。(学习视频分享:vue视频教程)一、局部样式当标签带有scopedattribute的时候,它的CSS只会应用到当前组件的元素上:hi.example{color:red;}二、深度选择器处于scoped样式中的选择器如果想要做更“深度”的选择,也即:影响到子组件,可以使用:deep()这个伪类:.a:deep(.b){/*...*/}通过v-html创建的DOM内容不会被

CSS网页背景图设计:创建各种背景图样式和效果CSS网页背景图设计:创建各种背景图样式和效果Nov 18, 2023 am 08:38 AM

CSS网页背景图设计:创建各种背景图样式和效果,需要具体代码示例摘要:在网页设计中,背景图是一种重要的视觉元素,它可以有效地增强页面的吸引力和可读性。本文将介绍一些常见的CSS背景图设计样式和效果,并提供相应的代码示例。读者可以根据自己的需求和喜好来选择和应用这些背景图样式和效果,以达到更好的视觉效果和用户体验。关键词:CSS,背景图,设计样式,效果,代码示

Vue报错:无法正确使用v-bind绑定class和style,怎样解决?Vue报错:无法正确使用v-bind绑定class和style,怎样解决?Aug 26, 2023 pm 10:58 PM

Vue报错:无法正确使用v-bind绑定class和style,怎样解决?在Vue开发中,我们经常会用到v-bind指令来动态绑定class和style,但是有时候我们可能会遇到一些问题,如无法正确使用v-bind绑定class和style。在本篇文章中,我将为你解释这个问题的原因,并提供解决方案。首先,让我们先了解一下v-bind指令。v-bind用于将V

使用:nth-last-child(2)伪类选择器选择倒数第二个子元素的样式使用:nth-last-child(2)伪类选择器选择倒数第二个子元素的样式Nov 20, 2023 am 11:22 AM

使用:nth-last-child(2)伪类选择器选择倒数第二个子元素的样式,需要具体代码示例在CSS中,伪类选择器是一种非常强大的工具,可以用来选择文档树中特定的元素。其中之一就是:nth-last-child(2)伪类选择器,它可以选择倒数第二个子元素并对其应用样式。首先,让我们来创建一个示例HTML文档,以便我们可以在其中使用这个伪类选择器。以

See all articles

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 Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools