将document.body.className设置为变量

Cho*_*hoy 4 javascript variables

这是我的代码,用于在用户单击链接时切换我的body标记的类.

function switchBodyColor() {
    if (document.body.className == 'blue')
        document.body.className = 'red';
    else if (document.body.className == 'red')
        document.body.className = 'green';
    else if (document.body.className == 'green')
        document.body.className = 'blue';
}
Run Code Online (Sandbox Code Playgroud)

我想将结果颜色设置为一个名为bodyColor的变量.因此,如果body类是"蓝色"并且用户单击并将其切换为"red",我想将红色存储为变量(bodyColor)以供稍后用于其他用途.或者更好的是,将document.body.className设置为变量本身,然后使用该变量切换switchBodyColor()函数中的document.body.className.

我想到了以下几点:

    if (document.body.className == 'blue')
        document.body.className = 'red',
        var bodyColor = red;
Run Code Online (Sandbox Code Playgroud)

要么

var bodyColor = document.body.className
Run Code Online (Sandbox Code Playgroud)

将body类设置为变量.

当然,这两种选择都不起作用.^ _ ^; 我怎样才能完成上述任何一项(或两项)?

Jef*_*f B 10

您需要将变量设为全局变量:

var bodyColor = 'red';  // Global var, initialized to your first color class

function switchBodyColor() {
    if (document.body.className == 'blue')
        document.body.className = 'red';
    else if (document.body.className == 'red')
        document.body.className = 'green';
    else if (document.body.className == 'green')
        document.body.className = 'blue';


    bodyColor = document.body.className;
    alert(bodyColor);
}
Run Code Online (Sandbox Code Playgroud)

在另一个示例中,您还需要在颜色字符串周围加上引号:

 bodyColor = "red";
Run Code Online (Sandbox Code Playgroud)



另一种方法可能是为您的颜色类编号,这将允许您使用简单的算术来更改您的类,并允许您轻松更改您循环的类的数量.

var colorNum = 0;
var totalColors = 3;

function switchBodyColor() {
    colorNum = (colorNum+1)%totalColors;
    document.body.className = 'color'+colorNum;
}
Run Code Online (Sandbox Code Playgroud)

你将是:

.color0 { background-color: blue; }
.color1 { background-color: red; }
.color2 { background-color: green; }
Run Code Online (Sandbox Code Playgroud)

或者你的颜色类定义是什么.


Dou*_*ner 5

您可以将颜色存储在数组中,然后通过操作始终使用数组中的第一种颜色作为当前颜色:

var bodyColors = ['blue','red','green'];

function switchBodyColor(){
   bodyColors.push(bodyColors.shift()); // Move first item to the end
   document.body.className = bodyColors[0];
}
Run Code Online (Sandbox Code Playgroud)

然后在您的应用中需要它的任何地方,只需致电:

bodyColors[0]; // Will refer to the current body class
Run Code Online (Sandbox Code Playgroud)

可选检查初始状态

前面的代码假定您的body元素始终以blue.如果不是这种情况,您可以在switchBodyColor()函数正下方添加此一次性运行代码:

for(var i=0; i<bodyColors.length; i++){
   if(document.body.className == bodyColors[i]) break;
   bodyColors.push(bodyColors.shift());
}
Run Code Online (Sandbox Code Playgroud)

附加说明

由于您希望颜色始终以相同的顺序旋转,因此使用数组是有意义的,因为它的顺序始终受到尊重.然而,由于indexOf至少在IE7及以下没有" ",我们无法将当前颜色与其在阵列中的位置相匹配而没有循环.

这是Array.shiftArray.push命令发挥作用的地方.Array.shift删除数组中的第一个元素,并返回它.Array.push获取传递给它的内容,并将其"推"到数组的末尾.通过将两种方法结合在一起,我们可以获取第一项并将其移至最后,创建一个类型的轮播:

var c = [1,2,3];
c.push(c.shift());
console.log(c); // Outputs [2,3,1]
c.push(c.shift());
console.log(c); // Outputs [3,1,2]
c.push(c.shift());
console.log(c); // Outputs [1,2,3]
Run Code Online (Sandbox Code Playgroud)

因此,订单始终受到尊重,第一个元素始终设置为我们想要的,因此bodyColor[0]始终是当前颜色.