以下是一个使用JavaScript和CSS实现按钮点击后悬停生效的代码示例:
HTML代码:
CSS代码:
#myButton {
background-color: blue;
color: white;
padding: 10px;
border: none;
cursor: pointer;
}
#myButton:hover {
background-color: red;
}
JavaScript代码:
document.getElementById("myButton").addEventListener("click", function() {
document.getElementById("myButton").classList.add("hover");
});
document.getElementById("myButton").addEventListener("mouseout", function() {
document.getElementById("myButton").classList.remove("hover");
});
这段代码首先将按钮的背景色设置为蓝色,当鼠标悬停在按钮上时,背景色变为红色。当按钮被点击后,使用JavaScript添加一个名为"hover"的类,这样鼠标移开按钮时仍然保持红色背景。
上一篇:按钮消失,代码不再起作用。
下一篇:按钮悬停过渡持续时间