Jquery悬停不会显示警报

Ice*_*awg 2 jquery

我正在使用此代码,但它没有显示警报;

$(document).ready(function() {
    $(".one").hover( 
        alert("hello");
    });
});
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

kri*_*ath 5

你在那里缺少一个函数声明。hover接受一个函数(或两个函数)作为参数。将您的代码更改为:

$(".one").hover( function () {
  alert("hello");
}, function() {
  alert("And we're out");
});
Run Code Online (Sandbox Code Playgroud)

第一个函数用于当您将鼠标悬停在 上时发生的操作.one。第二个是当您将鼠标悬停在 之外时.one。你也可以这样做:

$(".one").hover(inWeGo, outWeCome);

function inWeGo() {
  alert("hello");
}

function outWeCome() {
  alert("And we're out");
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用mouseovermouseout事件:

$(".one").on({
  "mouseover" : inWeGo,
  "mouseout" : outWeCome
});
Run Code Online (Sandbox Code Playgroud)

hover是这两种方法的简写。

文档中的更多信息:


Dan*_*Dan 5

$(".one").hover(function() {
    alert("hello");
});
Run Code Online (Sandbox Code Playgroud)