[英]Creating Pre-Made CSS Variables With JavaScript
Let's say I'm working on a large page. 假设我正在处理一个大页面。 I want to predefine some CSS styles as variables in JavaScript that I can use later.
我想将一些CSS样式预先定义为JavaScript中的变量,以便以后使用。 Is it possible?
可能吗? What would the syntax be?
语法是什么?
I'm trying to do something like this: 我正在尝试做这样的事情:
var box = document.querySelector('.box');
var styleColor = style.color;
var red = "red"
box.onclick = function(){styleColor.red;}
I'm aware you can do it like this: 我知道您可以这样做:
var box = document.querySelector('.box');
box.onclick = function(){box.style.color = "red";}
But I want to cache the CSS so It can be re-used later. 但是我想缓存CSS,以便以后可以重复使用。 Is this possible, if so what would the syntax look like?
如果可能,语法是什么样?
Write your css
in a normal css file. 将您的
css
写入普通的CSS文件中。 For instance: 例如:
.myRedClass { color: red; }
Then append a class to elements you want to be red in javascript as follows 然后将一个类附加到您希望在javascript中变为红色的元素,如下所示
var element = document.getElementById(elementId);
element.className += " " + "myRedClass";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.