Setting background color with JavaScript breaks the CSS hover behavior

前端 未结 4 1338
你的背包
你的背包 2021-01-15 14:47

I\'m trying to create a menu where the currently selected (clicked) element has a different background color than the other elements (I\'m trying to achieve this using JavaS

4条回答
  •  梦毁少年i
    2021-01-15 15:24

    Just for a more simple answer, in able to just re-enable css rules just have it toggle between the color and "", so

    document.getElementById("id").style.backgroundColor = "rgb(255, 125, 15)";
    

    would be if the element wasn't already colored via javascript. Now, if your element was already colored the code would look like this:

    document.getElementById("id").style.backgroundColor = "";
    

    That re-enables CSS so then your selectors will work.

提交回复
热议问题