javascript 其他切换

javascript else of toggle

好的,我正在做项目,我在 2 号,我遇到了切换问题,但我似乎无法让它工作。

document.getElementsByClassName("buttons")[1].onmousedown=
    function(){
        colorchange();
    };
}
function colorchange() {

    /*var background = function blue(){document.getElementsByClassName("buttons")[1].style.border;*/
    if (getElementsByClassName("buttons")[1].style.border == "5px #c90 solid") {
        document.getElementsByClassName("buttons")[1].style.border="5px blue solid";
    } else {
        if (getElementsByClassName("buttons")[1] == "5px blue solid") {
        document.getElementsByClassName("buttons")[1].style.background = "5px #c90 solid";
    }

}
}

你所在的行:

if (getElementsByClassName("buttons")[1] == "5px blue solid")

应该是:

if (getElementsByClassName("buttons")[1].style.border == "5px solid blue")

这里有一个方法:

window.onload = init;
var stateTwo = false, colorsThree = ['#cc9900','#1e3a03','#611790'];

function init(){
    var btns = document.getElementsByClassName("buttons");
    btns[0].onmousedown = btns[0].onmouseup = funcOne;
    btns[1].onclick = funcTwo;
    btns[2].onclick = funcThree;
}

function funcOne(e){
    this.style.background = ( e.type == 'mouseup' )? '#336600' : '#bd270a';
}

function funcTwo(e){
    stateTwo = !stateTwo;
    this.style.borderColor = stateTwo ? '#0c1583' : '#cc9900';
}

function funcThree(){
    colorsThree.push( colorsThree.shift() );
    this.style.borderColor = colorsThree[0];
}

JS Fiddle Demo