如何将5个单击功能转换为具有更改值的功能

ivo*_*ign 0 javascript css jquery

如何将5个单击功能转换为一个?例如

$("div#styleswitcher a.green").click(function(){
$("container").css({'background' : 'green'});

)

$("div#styleswitcher a.blue").click(function(){
$("container").css({'background' : 'blue'});

)

$("div#styleswitcher a.red").click(function(){
$("container").css({'background' : 'red'});

)

$("div#styleswitcher a.yellow").click(function(){
$("container").css({'background' : 'yellow'});

)
Run Code Online (Sandbox Code Playgroud)

Phi*_*ert 6

想到这一点:

$("#styleswitcher a").click(function ()
{
    $("container").css("background-color", this.className);
});
Run Code Online (Sandbox Code Playgroud)

假设背景颜色应该是类名,如您的示例所示