Tag: css variables

通过JavaScript访问CSS自定义属性(也称为CSS变量)

你如何使用JavaScript(plain或jQuery)获取和设置CSS自定义属性(使用样式表中的var(…)访问的属性)? 这是我不成功的尝试:单击按钮更改通常的font-weight属性,但不更改自定义–mycolor属性: body { –mycolor: yellow; background-color: var(–mycolor); } Let’s try to make this text bold and the background red. Plain JS jQuery function plain_js() { document.body.style[‘font-weight’] = ‘bold’; document.body.style[‘–mycolor’] = ‘red’; }; function jQuery_() { $(‘body’).css(‘font-weight’, ‘bold’); $(‘body’).css(‘–mycolor’, ‘red’); }