)我有一個用于更改網(wǎng)站上按鈕的 CSS 類的代碼:window.onload = function(){ var button = document.getElementById("button"); button.addEventListener("mouseenter", function(){ button.classList.add("buttonclass1"); }); button.addEventListener("mouseleave", function(){ button.classList.add("bnative1"); })}I want to make like this: if(mouseenter){ button.addEventListener("mouseenter", function(){ button.classList.add("buttonclass1"); });}else{ button.addEventListener("mouseleave", function(){ button.classList.add("bnative1");}但這不起作用,通過改變這個類我想實現(xiàn)改變顏色。當鼠標放在按鈕上時,它會發(fā)生變化,但當鼠標離開按鈕時,它不會變回來。我該如何使用JS來實現(xiàn)呢?
2 回答

狐的傳說
TA貢獻1804條經(jīng)驗 獲得超3個贊
你忘了在休假時切換開關...
window.onload = function(){
var button = document.getElementById("button");
button.addEventListener("mouseenter", function(){
button.classList.remove("bnative1");
button.classList.add("buttonclass1");
});
button.addEventListener("mouseleave", function(){
button.classList.remove("buttonclass1");
button.classList.add("bnative1");
})
}

桃花長相依
TA貢獻1860條經(jīng)驗 獲得超8個贊
或者,如果您想要的只是添加顏色,則可以使用純 css 來完成此操作。
.hover-bg-blue:hover {
background-color: blue;
color: white;
}
<button class="hover-bg-blue">Hello</button>
- 2 回答
- 0 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消