Javascript 开关箱直接连接到端箱

Javascript 开关箱直接连接到端箱,javascript,html,css,Javascript,Html,Css,当我运行这段代码时,它将贯穿整个开关直到结束,即使前面的一个案例是匹配的。我知道这段代码很难看,但在我添加一个案例之前,它是有效的,现在它不起作用了 function setColours() { var operatorName = document.getElementById("operator-header").innerHTML; console.log(operatorName); switch(operatorName)

当我运行这段代码时,它将贯穿整个开关直到结束,即使前面的一个案例是匹配的。我知道这段代码很难看,但在我添加一个案例之前,它是有效的,现在它不起作用了

    function setColours() {
        var operatorName = document.getElementById("operator-header").innerHTML; 
        console.log(operatorName);

        switch(operatorName) {
                case "Great Western Railway":
                    document.getElementById("section2").style.backgroundColor = "darkgreen";
                    document.getElementById("section2").style.color = "white";
                case "c2c":
                    document.getElementById("section2").style.backgroundColor = "white";
                    document.getElementById("section2").style.color = "purple";
                case "Arriva Trains Wales":
                    document.getElementById("section2").style.backgroundColor = "rgb(0,190,206)";
                    document.getElementById("section2").style.color = "white";
                case "Chiltern Railways":
                    document.getElementById("section2").style.backgroundColor = "white";
                    document.getElementById("section2").style.color = "rgb(45,106,140)";
                    document.getElementById("horizontal-line").style.borderTop = "1px solid #53c2f0"
                case "CrossCountry":
                    document.getElementById("section2").style.backgroundColor = "rgb(136,0,56)";
                    document.getElementById("section2").style.color = "white";
                case "East Midlands Trains":
                    document.getElementById("section2").style.backgroundColor = "rgb(17,114,195)";
                    document.getElementById("section2").style.color = "rgb(246,198,42)";
                    document.getElementById("horizontal-line").style.border = "1px solid #cc1e05";
                case "Great Western Railway":
                    document.getElementById("section2").style.backgroundColor = "darkgreen";
                    document.getElementById("section2").style.color = "white";
                    document.getElementById("horizontal-line").style.border = "1px solid #ffffff";
                case "Grand Central":
                    document.getElementById("section2").style.backgroundColor = "black";
                    document.getElementById("section2").style.color = "white";
                    document.getElementById("horizontal-line").style.border = "1px solid #fe8238";
                case "Greater Anglia":
                    document.getElementById("section2").style.backgroundColor = "rgb(204, 204, 204)";
                    document.getElementById("section2").style.color = "rgb(204,33,29)";
                    document.getElementById("horizontal-line").style.border = "1px solid #ffffff";
                case "Heathrow Express":
                    document.getElementById("section2").style.backgroundColor = "white";
                    document.getElementById("section2").style.color = "rgb(109,56,122)";
                    document.getElementById("horizontal-line").style.border = "1px solid #d4d4d4";
                case "West Midlands Trains":
                    document.getElementById("section2").style.backgroundColor = "rgb(90,30,79)";
                    document.getElementById("section2").style.color = "rgb(240,140,38)";
                    document.getElementById("horizontal-line").style.border = "1px solid #ffffff";
                case "London Overground":
                    document.getElementById("section2").style.backgroundColor = "white";
                    document.getElementById("section2").style.color = "rgb(240,140,38)";
                    document.getElementById("horizontal-line").style.border = "1px solid #1172c3";
                case "Merseyrail":
                    document.getElementById("section2").style.backgroundColor = "rgb(250,203,51)";
                    document.getElementById("section2").style.color = "white";
                    document.getElementById("horizontal-line").style.border = "1px solid #ffffff"; 

运行此操作时,控制台输出正确的运算符,但它仍然默认为结束状态

Add'break;'在那件案子的最后

例:


尝试在每个新案例行之前添加一个中断,例如

     switch(operatorName) {
                case "Great Western Railway":
                    document.getElementById("section2").style.backgroundColor = "darkgreen";
                    document.getElementById("section2").style.color = "white";
break;
                case "c2c":
                    document.getElementById("section2").style.backgroundColor = "white";
                    document.getElementById("section2").style.color = "purple";
break;

中断用于结束案例块并开始新的案例块,它将它们(案例块)分开,以便计算机可以分别识别每个案例。

您需要为每个案例添加
中断
,如果未选择任何案例,还应具有
默认
案例

在您的代码中,将应用最后一种情况

函数setcolors(){
var operatorName=document.getElementById(“运算符标题”).innerHTML;
console.log(operatiorname);
开关(操作员名称){
案例“西部大铁路”:
document.getElementById(“section2”).style.backgroundColor=“暗绿色”;
document.getElementById(“section2”).style.color=“白色”;
打破
案例“c2c”:
document.getElementById(“section2”).style.backgroundColor=“白色”;
document.getElementById(“section2”).style.color=“紫色”;
打破
案例“抵达威尔士列车”:
document.getElementById(“section2”).style.backgroundColor=“rgb(0190206)”;
document.getElementById(“section2”).style.color=“白色”;
打破
案例“Chiltern铁路”:
document.getElementById(“section2”).style.backgroundColor=“白色”;
document.getElementById(“section2”).style.color=“rgb(45106140)”;
document.getElementById(“水平线”).style.borderTop=“1px solid#53c2f0”
打破
“跨国”案:
document.getElementById(“section2”).style.backgroundColor=“rgb(136,0,56)”;
document.getElementById(“section2”).style.color=“白色”;
打破
案例“东米德兰火车”:
document.getElementById(“section2”).style.backgroundColor=“rgb(17114195)”;
document.getElementById(“section2”).style.color=“rgb(246198,42)”;
document.getElementById(“水平线”).style.border=“1px solid#cc1e05”;
案例“西部大铁路”:
document.getElementById(“section2”).style.backgroundColor=“暗绿色”;
document.getElementById(“section2”).style.color=“白色”;
document.getElementById(“水平线”).style.border=“1px solid#ffffff”;
案例“大中环”:
document.getElementById(“section2”).style.backgroundColor=“黑色”;
document.getElementById(“section2”).style.color=“白色”;
document.getElementById(“水平线”).style.border=“1px solid”#fe8238”;
“大英吉利亚”案:
document.getElementById(“section2”).style.backgroundColor=“rgb(204204204)”;
document.getElementById(“section2”).style.color=“rgb(204,33,29)”;
document.getElementById(“水平线”).style.border=“1px solid#ffffff”;
案例“希思罗快车”:
document.getElementById(“section2”).style.backgroundColor=“白色”;
document.getElementById(“section2”).style.color=“rgb(109,56122)”;
document.getElementById(“水平线”).style.border=“1px solid#d4”;
案例“西米德兰兹列车”:
document.getElementById(“section2”).style.backgroundColor=“rgb(90,30,79)”;
document.getElementById(“section2”).style.color=“rgb(240140,38)”;
document.getElementById(“水平线”).style.border=“1px solid#ffffff”;
案例“伦敦地上”:
document.getElementById(“section2”).style.backgroundColor=“白色”;
document.getElementById(“section2”).style.color=“rgb(240140,38)”;
document.getElementById(“水平线”).style.border=“1px solid#1172c3”;
“默西铁路”案:
document.getElementById(“section2”).style.backgroundColor=“rgb(250203,51)”;
document.getElementById(“section2”).style.color=“白色”;
document.getElementById(“水平线”).style.border=“1px solid#ffffff”;
打破
违约:

//如果没有案例,请选择在每个案例末尾插入
break;
     switch(operatorName) {
                case "Great Western Railway":
                    document.getElementById("section2").style.backgroundColor = "darkgreen";
                    document.getElementById("section2").style.color = "white";
break;
                case "c2c":
                    document.getElementById("section2").style.backgroundColor = "white";
                    document.getElementById("section2").style.color = "purple";
break;