为什么onChange每个值只能工作一次

时间:2019-04-05 11:07:43

标签: javascript html

每次加载网页时,onchange方法均有效,但每个值仅适用一次。当选择夏天时,添加班级,当我选择冬天时,添加班级,但是如果我再次选择夏天,它将不起作用。为什么会这样?

 const landingBGR = document.querySelector(".landing-wrapper");
    const landingBTN = document.getElementById("l-btn");

    function selectedSeason(season) {
    
    switch(season) {
        case "summer":
            landingBTN.setAttribute("href","summer.html"); 
            landingBGR.classList.add("summer-bgr");
            break;

        case "winter":
            landingBTN.setAttribute("href","winter.html");
            landingBGR.classList.add("winter-bgr");
            break;
         }
    }
<select name="" id="landing-drop" onchange="oninput(this.value)">
            <option value="">Ireland in the four Seasons</option>
            <option value="summer">Summer</option>
            <option value="winter">Winter</option>
        </select>

1 个答案:

答案 0 :(得分:1)

在选择新选项时尝试删除旧类。

landingBGR.classList.remove("summer-bgr");

landingBGR.classList.remove("winter-bgr");

您可能需要

if(landingBGR.classList.contains("classname"))