作者:圊渘湜壞亾 | 来源:互联网 | 2023-01-09 10:27
Inmyapplication,IvegotsomeparametersthatIwantedtoputinCSSvariablesusingjQuery.And
In my application, I've got some parameters that I wanted to put in CSS variables using jQuery. And I wanted to read them too.
在我的应用程序中,我有一些参数,我想使用jQuery放入CSS变量。我也想读它们。
I was having trouble to read the values back of the CSS variables and tried many things… before I found a solution in the “Questions that may already have your answer” while I was typing my question.
我在查看CSS变量的值时遇到了麻烦并尝试了很多事情......在我输入问题时,我在“可能已经有你的答案的问题”中找到了解决方案。
Anyway, I attached a snippet, because I need to know:
⋅⋅⋅ Why the method 1 isn't working ?
⋅⋅⋅ Is there a way to get the CSS var value using jQuery ?
无论如何,我附上了一个片段,因为我需要知道:⋅⋅⋅为什么方法1不起作用? ⋅⋅⋅有没有办法使用jQuery获取CSS var值?
I feel like it lacks an easy solution to handle CSS vars… Am I wrong ?
Of course I'll use the Javascript solution if there isn't any way. But I'd like to be sure about it.
Thanks in advance for any answer.
我觉得它缺乏处理CSS变量的简单解决方案......我错了吗?当然,如果没有任何办法我会使用Javascript解决方案。但我想确定一下。提前感谢您的回答。
// This method doesn't work for writing CSS var.
$(":root").css("--color1", "red");
console.log(".css method on “:root” :", $(":root").css("--color1"));
// This methods works for writing CSS var:
$(":root")[0].style.setProperty("--color2", "lime");
console.log("[0].style method on “:root” :", $(":root")[0].style.getPropertyValue('--color2'));
#p1 {
background-color: var(--color1);
}
#p2 {
background-color: var(--color2);
}
p with background --color1
p with background --color2
2 个解决方案
1
As BoltClock mentions in their answer, jQuery added support for CSS variables starting from version 3.2.0.
正如BoltClock在他们的回答中提到的,jQuery从版本3.2.0开始添加了对CSS变量的支持。
But if you can't upgrade to the higher version for some reason, you could still extend jQuery's $.fn.css
method to work with the custom properties.
但是如果由于某种原因无法升级到更高版本,您仍然可以扩展jQuery的$ .fn.css方法来处理自定义属性。
So here's my attempt at implementing a simple extension that checks if the property being modified is a Custom property or not (by checking that it begins with two hyphens). If it does, the custom property is modified using plain JS, else it calls the original implementation of $.fn.css
.
所以这是我尝试实现一个简单的扩展,它检查被修改的属性是否是Custom属性(通过检查它是否以两个连字符开头)。如果是,则使用普通JS修改自定义属性,否则它调用$ .fn.css的原始实现。
(function() {
var originalFnCss = $.fn.css;
$.fn.css = function(prop, val) {
// Check if the property being set is a CSS Variable.
if (prop.indexOf("--") === 0) {
if(val) {
// Set the value if it is provided.
for(var idx = 0; idx
Note: At the moment I have tested this extension with jQuery 1.11.1, 2.2.4 and 3.1.1, but do let me know if you find any bugs, or if you have any suggestions.
注意:目前我已经使用jQuery 1.11.1,2.2.4和3.1.1测试了此扩展,但是如果您发现任何错误或者您有任何建议,请告诉我。
Now you just need to add the extension just after importing jQuery, or at any point before $.fn.css
is invoked. Here's the working snippet:
现在你只需要在导入jQuery之后添加扩展,或者在调用$ .fn.css之前的任何时候添加扩展。这是工作片段:
(function() {
var originalFnCss = $.fn.css;
$.fn.css = function(prop, val) {
// Check if the property being set is a CSS Variable.
if (prop.indexOf("--") === 0) {
if(val) {
// Set the value if it is provided.
for(var idx = 0; idx
#p1 {
background-color: var(--color1);
}
#p2 {
background-color: var(--color2);
}
p with background --color1
p with background --color2