jQuery mouseleave()方法
jQuery mouseleave()方法是一个内置的方法,当鼠标指针离开选定的元素时,它就会发挥作用。
语法:
$(selector).mouseleave(function)
参数:该方法接受单参数function,这是可选的。它用于指定当鼠标离开事件被调用时要运行的函数。
例子1:这个例子说明了jQuery中的mouseleave()方法。
<!DOCTYPE html>
<html>
<head>
<title>The mouseleave Method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!-- jQuery code to show the working of this method -->
<script>
(document).ready(function () {
("p").mouseleave(function () {
$("p").css("background-color", "lightgreen");
});
});
</script>
<style>
body {
width: 300px;
padding: 40px;
height: 30px;
border: 2px solid green;
font-weight: bold;
font-size: 20px;
}
</style>
</head>
<body>
<!-- move over this paragraph and see the change -->
<p>Welcome to GeeksforGeeks!</p>
</body>
</html>
输出:
例子2:在这个例子中,当我们把鼠标移到该段上时,会有一个弹出窗口。
<!DOCTYPE html>
<html>
<head>
<title>The mouseleave Method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!-- jQuery code to show the working of this method -->
<script>
(document).ready(function () {
("p").mouseleave(function () {
alert("Mouse moved over this paragraph");
});
});
</script>
<style>
body {
width: 300px;
padding: 40px;
height: 30px;
border: 2px solid green;
font-weight: bold;
font-size: 20px;
}
</style>
</head>
<body>
<!-- move over this paragraph and pop-up will come -->
<p>Welcome to GeeksforGeeks!</p>
</body>
</html>
输出: