如何使用Javascript重新定义CSS类 [英] How to redefine CSS classes with Javascript
问题描述
假设我们定义了一个应用于页面上各种元素的CSS类。
Let's say we have defined a CSS class that is being applied to various elements on a page.
colourful
{
color: #DD00DD;
background-color: #330033;
}
人们投诉颜色,他们不喜欢粉红色/紫色。所以你想让他们能够随意改变风格,他们可以选择自己喜欢的颜色。你有一个调色Javascript函数的小选色器小部件:
People have complained about the colour, that they don't like pink/purple. So you want to give them the ability to change the style as they wish, and they can pick their favourite colours. You have a little colour-picker widget that invokes a Javascript function:
function changeColourful(colorRGB, backgroundColorRGB)
{
// answer goes here
}
函数?
意图是当用户在颜色选择器上选择一个新颜色时,所有元素都会包含 class =colorful code>将改变他们的样式。
The intent being that when the user picks a new colour on the colour-picker all the elements with class="colourful"
will have their style changed.
推荐答案
我不知道直接操作类,有效地做同样的事情。
I don't know about manipulating the class directly, but you can effectively do the same thing. Here's an example in jQuery.
$('.colourful').css('background-color', 'purple').css('color','red');
在纯javascript中,你必须做更多的工作。
In plain javascript, you would have to do more work.
这篇关于如何使用Javascript重新定义CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!