简体   繁体   English

如何将css class从css文件添加到js变量

[英]How to add css class from css file to a js variable

I have color-switcher css files.It contains css classes.The class are for colors.我有颜色切换器 css 文件。它包含 css 类。class 用于 colors。

Like this:像这样:

color-2.css:颜色-2.css:

/* Template Color Default  #ff6600 = rgba(255, 102, 0, */

/* TEXT COLOR */
nav.navbar.bootsnav.menu-style1 ul.dropdown-menu.megamenu-content .title,
.upcoming-event .clock.flip-clock-wrapper ul li a div div.inn,
.pagination>li>a,
.fancybox-gallery-slider .owl-prev,
.fancybox-gallery-slider .owl-next,
.team-icon a,
.twitter.style2 ul li a,
.inner-conraimer-details h1,
.text-thm2 {
  color: #ff6600 !important;
}

I must add these css classes to a js variable and access this js variable from a js file.我必须将这些 css 类添加到 js 变量并从 js 文件访问此 js 变量。

I have below js code:我有以下js代码:

 if (Bcolor === 'rgb(22, 160, 133)') {

            $("ul.colors .color1").on('click', function() {
                node.style.setProperty("background-color", "red", "important");
                document.querySelector(".ulockd-btn-white").classList.add("mystyle");

                var style = document.createElement('style');
                style.type = 'text/css';
                style.innerHTML = '.cssClass:hover { background-color:  cyan!important ; } .colorful-buton-class:hover { background-color:  cyan!important ; }  .colorful-buton-class > * > * > [role=button] { background-color:  cyan!important ; }  .wpforms-form button[type=submit]:hover { background-color:cyan!important; }  ';
                document.getElementsByTagName('head')[0].appendChild(style);

                return false;
            });

            $("ul.colors .color2").on('click', function() {
                node.style.setProperty("background-color", "pink", "important");



                document.querySelector(".ulockd-btn-white").classList.add("mystyle");

                var style = document.createElement('style');
                style.type = 'text/css';
                style.innerHTML = '.cssClass:hover { background-color:  orange!important ; } .colorful-buton-class:hover { background-color:  orange!important ; }  .colorful-buton-class > * > * > [role=button] { background-color:  orange!important ; }  .wpforms-form button[type=submit]:hover { background-color:orange!important; }  ';
                document.getElementsByTagName('head')[0].appendChild(style);


                return false;
            });

        }


    });

there are some css classes in this js class:这个js class中有一些css类:

 style.innerHTML = '.cssClass:hover { background-color:  cyan!important ; } .colorful-buton-class:hover { background-color:  cyan!important ; }  .title-bottom:before {background-color: red!important; } .mt-separator::before, .mt-separator:before, .mt-separator::after {background-color: red!important; } .ulockd-btn-thm2:hover { background-color:cyan!important; } .wpforms-form button[type=submit]:hover { background-color:cyan!important; } .elementor-icon, .elementor-heading-title { color:red!important; }  ';
              

I must add the css file content to this variable.我必须将 css 文件内容添加到此变量。

how can I do it?我该怎么做?

you can use jquery.你可以使用 jquery。

I am new at js and jquery.我是 js 和 jquery 的新手。

Keep all styles as classes in one css file.将所有 styles 作为类保存在一个 css 文件中。 Then, import that css file to HTML file.然后,将该 css 文件导入到 HTML 文件。 Don't put styles inside JS.不要把 styles 放在 JS 里面。 So, your all style classes will be in one css file.因此,您的所有样式类都将在一个 css 文件中。 Then, you can add style classes you want in click functions separately.然后,您可以在点击功能中单独添加您想要的样式类。 You can use following code to add class.您可以使用以下代码添加 class。

var element = document.getElementById("myDIV");
element.classList.add("your_style_class");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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