Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改JS中的类属性-更好的性能_Javascript_Jquery_Css - Fatal编程技术网

Javascript 更改JS中的类属性-更好的性能

Javascript 更改JS中的类属性-更好的性能,javascript,jquery,css,Javascript,Jquery,Css,我有两个类需要动态更改颜色属性。 问题是我写的代码非常慢,因为这些类在我的代码中出现了100多次 有什么建议可以提高代码的性能吗 if (mainColor) { var elColors = document.querySelectorAll('.color') elColors.forEach(el => el.style.color = mainColor) var elBgColors = document.querySelectorAll('.bg-col

我有两个类需要动态更改颜色属性。 问题是我写的代码非常慢,因为这些类在我的代码中出现了100多次

有什么建议可以提高代码的性能吗

if (mainColor) {
    var elColors = document.querySelectorAll('.color')
    elColors.forEach(el => el.style.color = mainColor)
    var elBgColors = document.querySelectorAll('.bg-color')
    elBgColors.forEach(el => el.style.backgroundColor = mainColor)
}

(主颜色来自用户输入)

要获得高性能,请不要为每种颜色设置样式

只需将一个新类分配给父节点

例如:

以动态方式-JavaScript:

var styles = document.querySelector('head style#dynamic'),
    sheet;

if (!styles) {
    styles = document.createElement('style');
    styles.id = 'dynamic';
    document.querySelector('head').appendChild(styles);
}

sheet = styles.sheet;
sheet.insertRule("body.mainTheme .color { color: red; }", 0);
sheet.insertRule("body.mainTheme .bg-color { background-color: blue }", 0);

if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
// jQuery: $('body').addClass('mainTheme');
if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
.mainTheme .color {
    color: red;
}
.mainTheme .bg-color {
    background-color: blue
}
以静态方式-JavaScript部分:

var styles = document.querySelector('head style#dynamic'),
    sheet;

if (!styles) {
    styles = document.createElement('style');
    styles.id = 'dynamic';
    document.querySelector('head').appendChild(styles);
}

sheet = styles.sheet;
sheet.insertRule("body.mainTheme .color { color: red; }", 0);
sheet.insertRule("body.mainTheme .bg-color { background-color: blue }", 0);

if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
// jQuery: $('body').addClass('mainTheme');
if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
.mainTheme .color {
    color: red;
}
.mainTheme .bg-color {
    background-color: blue
}
以静态方式-CSS部分:

var styles = document.querySelector('head style#dynamic'),
    sheet;

if (!styles) {
    styles = document.createElement('style');
    styles.id = 'dynamic';
    document.querySelector('head').appendChild(styles);
}

sheet = styles.sheet;
sheet.insertRule("body.mainTheme .color { color: red; }", 0);
sheet.insertRule("body.mainTheme .bg-color { background-color: blue }", 0);

if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
// jQuery: $('body').addClass('mainTheme');
if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
.mainTheme .color {
    color: red;
}
.mainTheme .bg-color {
    background-color: blue
}

为了获得高性能,不要为每一个设置样式

只需将一个新类分配给父节点

例如:

以动态方式-JavaScript:

var styles = document.querySelector('head style#dynamic'),
    sheet;

if (!styles) {
    styles = document.createElement('style');
    styles.id = 'dynamic';
    document.querySelector('head').appendChild(styles);
}

sheet = styles.sheet;
sheet.insertRule("body.mainTheme .color { color: red; }", 0);
sheet.insertRule("body.mainTheme .bg-color { background-color: blue }", 0);

if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
// jQuery: $('body').addClass('mainTheme');
if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
.mainTheme .color {
    color: red;
}
.mainTheme .bg-color {
    background-color: blue
}
以静态方式-JavaScript部分:

var styles = document.querySelector('head style#dynamic'),
    sheet;

if (!styles) {
    styles = document.createElement('style');
    styles.id = 'dynamic';
    document.querySelector('head').appendChild(styles);
}

sheet = styles.sheet;
sheet.insertRule("body.mainTheme .color { color: red; }", 0);
sheet.insertRule("body.mainTheme .bg-color { background-color: blue }", 0);

if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
// jQuery: $('body').addClass('mainTheme');
if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
.mainTheme .color {
    color: red;
}
.mainTheme .bg-color {
    background-color: blue
}
以静态方式-CSS部分:

var styles = document.querySelector('head style#dynamic'),
    sheet;

if (!styles) {
    styles = document.createElement('style');
    styles.id = 'dynamic';
    document.querySelector('head').appendChild(styles);
}

sheet = styles.sheet;
sheet.insertRule("body.mainTheme .color { color: red; }", 0);
sheet.insertRule("body.mainTheme .bg-color { background-color: blue }", 0);

if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
// jQuery: $('body').addClass('mainTheme');
if (!document.body.className.match(/(^|\s)mainTheme(\s|$)/s)) {
    document.body.className = document.body.className + " mainTheme";
}
.mainTheme .color {
    color: red;
}
.mainTheme .bg-color {
    background-color: blue
}

虽然您已经接受了一个很好的答案,但值得添加一个利用CSS自定义属性的进一步替代方案;在这里,我们在一个地方更新了一个自定义属性(“CSS变量”),该更改反映在使用该自定义属性的任何地方:

函数更新颜色(主颜色){
if(主色){
让root=document.querySelector(':root');
root.style.setProperty('--customColor',mainColor);
}
}
document.querySelector('input[type=color]')).addEventListener('input',function(){
更新颜色(此.value);
});
:根目录{
--颜色:红色;
}
身体{
显示:网格;
网格模板列:重复(4,1fr);
栅隙:10px;
}
分色{
颜色:var(--customColor);
}
div.bg-color{
背景色:var(--customColor);
}

没课
“颜色”
没课
“背景色”
“背景色”
没课
“背景色”
没课
没课
没课
没课
没课
“背景色”
没课
“背景色”
“背景色”
“颜色”
“颜色”
没课
“背景色”
没课
“颜色”
没课
“颜色”
没课
“颜色”
“颜色”
“背景色”
“背景色”
“颜色”
“背景色”
没课
没课
没课
没课
“颜色”
“背景色”
“背景色”
没课
“颜色”
“颜色”
“颜色”
“背景色”
“背景色”
没课
“颜色”
没课
“颜色”
没课
没课
“背景色”
“颜色”
“背景色”
“颜色”
“颜色”
“颜色”
没课
没课
没课
“颜色”
“背景色”
“颜色”
“颜色”
“颜色”
“颜色”
“颜色”
“背景色”
“背景色”
“背景色”
“背景色”
“颜色”
“背景色”
没课
“背景色”
没课
“背景色”
“背景色”
没课
没课
“背景色”
“背景色”
没课
“颜色”
“颜色”
“颜色”
“颜色”
没课
“颜色”
“颜色”
“颜色”
“背景色”
“背景色”
没课
“颜色”
“颜色”
“背景色”
没课
“背景色”
“背景色”
“颜色”
没课
“颜色”
“背景色”
没课
“颜色”
“颜色”
没课
“背景色”
“背景色”
没课
“颜色”
“颜色”
“颜色”
没课
“颜色”
“背景色”
“颜色”
“颜色”
“背景色”
“背景色”
没课
“背景色”
没课
“背景色”
没课
“颜色”
“背景色”
“背景色”
“背景色”
“背景色”
“颜色”
“颜色”
“背景色”
“颜色”
“背景色”
没课
“颜色”
没课
没课
“颜色”
“背景色”
没课
“背景色”
“背景色”
“颜色”
“颜色”
没课
没课
没课
“背景色”
“背景色”
没课
“颜色”
没课
没课
没课
“背景色”
“背景色”
没课
没课
“颜色”
“背景色”
“背景色”
“背景色”
没课
“背景色”
“背景色”
“颜色”
“颜色”
没课
没课
“颜色”
没课
没课
“背景色”
没课
没课
没课
“颜色”
没课
“背景色”
“颜色”
没课
没课
“背景色”
没课
“颜色”
“背景色”
“颜色”
没课
没课
“背景色”
“颜色”
没课
没课
没课
“颜色”
“颜色”
“背景色”
“背景色”
“颜色”
“颜色”
没课
“颜色”
“颜色”
没课
“背景色”
没课
“颜色”
没课
“颜色”
“颜色”
“背景色”
没课
没课
“颜色”
“颜色”
没课
没课
“背景色”
“背景色”
没课
“背景色”
“背景色”
“背景色”
“背景色”
“颜色”
“颜色”
“背景色”
“颜色”
“颜色”
没课
没课
没课
没课
没课
“颜色”
“背景色”
“颜色”
没课
没课
没课
“颜色”
“颜色”
“颜色”
“颜色”
“颜色”
“背景色”
“背景色”
没课
“颜色”
“颜色”
“背景色”
没课
“颜色”
“颜色”
“背景色”
没课
没课
没课
“背景色”
“背景色”
“背景色”
“背景色”
“背景色”
“颜色”
“背景色”
“颜色”
没课
“背景色”
“背景色”
“背景色”
“背景色”
“颜色”
没课
“颜色”
“背景色”
“背景色”
没课
没课
“颜色”
“背景色”
没课
“背景色”
“颜色”
“背景色”
“颜色”
没课
“背景色”
“背景色”
“颜色”
“颜色”
没课
“背景色”
“颜色”
“颜色”
“颜色”
“背景色”
“背景色”

“bg color”
虽然您已经接受了一个很好的答案,但值得再添加一个利用CSS自定义属性的替代方案;在这里,我们在一个地方更新了一个自定义属性(“CSS变量”),该更改反映在使用该自定义属性的任何地方:

函数更新颜色(主颜色){
if(主色){
让root=document.querySelector(':root');
root.style.setProperty('--customColor',mainColor);
}
}
document.querySelector('input[type=color]')).addEventListener('input',function(){
更新颜色(此.value);
});
:根目录{
--颜色:红色;
}
身体{
显示:网格;
网格模板列:重复(4,1fr);
栅隙:10px;
}
分色{
颜色:var(--customColor);
}
div.bg-color{
背景色:var(--customColor);
}

没课
“颜色”
没课
“背景色”
“背景色”
没课
“背景色”
没课
没课
没课
没课
没课
“背景色”
没课
“背景色”
“背景色”
“颜色”
“颜色”
没课
“背景色”
没课
“颜色”
没课
“颜色”
没课
“颜色”
“颜色”
“背景色”
“背景色”
“颜色”
“背景色”
没课
没课
没课
没课
“颜色”
“背景色”
“背景色”
没课
“颜色”
“颜色”
“颜色”
“背景色”
“背景色”
没课
“颜色”
没课
“颜色”
没课
没课
“背景色”
“颜色”
“背景色”
“颜色”
“颜色”
“颜色”
没课
没课
没课
“颜色”
“背景色”
“颜色”
“颜色”
“颜色”
“颜色”
“颜色”
“bg-c