如何使用 jQuery 反转悬停状态?

Dre*_*res 1 javascript css jquery

我的作业要求我只使用 jquery。看起来不太实用,但我能力有限。我能够弄清楚如何执行悬停状态,但是当应用样式时,它会保持不变。所以检查一下这个..

$(".cta-button").css({
      background: "#476887",
      "text-align": "center",
      width: "173px",
      height: "40px",
      margin: "62px auto 33px",
      cursor: "pointer"
    });
    $(".cta-button").hover(function() {
      $(this).css("background-color", "#509de5");
    });
Run Code Online (Sandbox Code Playgroud)

当然,当我不再悬停时,我希望它恢复到原来的背景颜色。我该怎么做呢?谢谢!!

Mai*_*rix 5

您可以使用hoverjQuery 的方法轻松实现

如文档中所述,此方法可以接受一到两个参数,第一个称为 the handlerIn,可以翻译为悬停状态、鼠标进入等,handlerOut对应于“鼠标离开”

所以为了实现你想要的你可以尝试这样的事情

$('DOM_NODE').hover(mouseEnter, mouseLeave);
function mouseEnter() {
     // do something when the mouse enters the dom node 
};
function mouseLeave() {
     // do something when the mouse leaves the dom node
};
Run Code Online (Sandbox Code Playgroud)