如果另一个if语句为真,如何停止和if语句

时间:2016-04-11 06:12:59

标签: javascript

快一点如何更改此代码,以便当其中一个if语句变为true时,它会跳过其余的代码?

    if (Guitar > Percussion && Guitar > Brass && Guitar > Keyboard && Guitar > Woodwind) // Here I'm checking to see if Guitar is greater than the others
            {
            var guitarpage = document.getElementById("GuitarPage"); // If guitar is then get the Element ID "GuitarPage"
            guitarpage.style.display ="block"; // Then Display that page
            }

        if (Percussion > Guitar && Percussion > Brass && Percussion > Keyboard && Percussion > Woodwind)
            {
            var percussionpage = document.getElementById("PercussionPage");
            percussionpage.style.display ="block";

            }

        if (Brass > Guitar && Brass > Brass && Percussion > Keyboard && Brass > Woodwind) {
            var brasspage = document.getElementById("BrassPage");
            brasspage.style.display ="block";
            }

        if (Keyboard > Guitar && Keyboard > Keyboard && Percussion > Brass && Keyboard > Woodwind) {
            var keyboardpage = document.getElementById("KeyboardPage");
            keyboardpage.style.display ="block";
            }

        if (Woodwind > Guitar && Woodwind > Brass && Woodwind > Keyboard && Woodwind > Percussion) {
            var woodpage = document.getElementById("WoodwindPage");
            woodpage.style.display ="block";
                        }

        else {
            var nonepage = document.getElementById ("NoPage");
            nonepage.style.display ="block";

            }

再次感谢您的帮助

1 个答案:

答案 0 :(得分:0)

if (Guitar > Percussion && Guitar > Brass && Guitar > Keyboard && Guitar > Woodwind) // Here I'm checking to see if Guitar is greater than the others
            {
            var guitarpage = document.getElementById("GuitarPage"); // If guitar is then get the Element ID "GuitarPage"
            guitarpage.style.display ="block"; // Then Display that page
            }

else if (Percussion > Guitar && Percussion > Brass && Percussion > Keyboard && Percussion > Woodwind)
            {
            var percussionpage = document.getElementById("PercussionPage");
            percussionpage.style.display ="block";

            }

else if (Brass > Guitar && Brass > Brass && Percussion > Keyboard && Brass > Woodwind) {
            var brasspage = document.getElementById("BrassPage");
            brasspage.style.display ="block";
            }

else if (Keyboard > Guitar && Keyboard > Keyboard && Percussion > Brass && Keyboard > Woodwind) {
            var keyboardpage = document.getElementById("KeyboardPage");
            keyboardpage.style.display ="block";
            }

else if (Woodwind > Guitar && Woodwind > Brass && Woodwind > Keyboard && Woodwind > Percussion) {
            var woodpage = document.getElementById("WoodwindPage");
            woodpage.style.display ="block";
                        }

else {
            var nonepage = document.getElementById ("NoPage");
            nonepage.style.display ="block";

            }