如何使用 JavaScript(普通或 jQuery)获取和设置 CSS 自定义属性(通过样式表中的 var(…) 访问的属性)?
这是我不成功的尝试:单击按钮会更改通常的 font-weight 属性,但不会更改自定义 --mycolor 属性:
Let's try to make this text bold and the background red.
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您可以使用
document.body.style.setProperty('--name', value);:var bodyStyles = window.getComputedStyle(document.body); var fooBar = bodyStyles.getPropertyValue('--foo-bar'); //get document.body.style.setProperty('--foo-bar', newValue);//set