Jquery mouseenter()
mouseenter()方法将事件处理函数添加到HTML元素。当鼠标指针进入HTML元素时,将执行此功能。
当您在所选元素上输入鼠标光标时,它将触发mouseenter事件,并且一旦发生mouseenter事件,它就会执行mouseenter()方法来附加事件处理函数以运行。
此事件通常与mouseleave()事件一起使用。
语法:
它触发选定元素的mouseenter事件。
$(selector).mouseenter(function)
它为mouseenter事件添加了一个函数。
jQuery mouseenter()事件的参数
参数 |
说明 |
function |
这是一个可选参数。触发mouseenter事件时,它将自行执行。 |
jQuery mouseenter()事件的示例
让我们以一个示例来演示jQuery mouseenter()事件。
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#h1").mouseenter(function(){
$("div").text("鼠标移动到此标题上。").show().fadeOut(2000);
});
});
</script>
</head>
<body>
<h1 id="h1">移动到此标题上。</h1>
<div></div>
</body>
</html>
输出:
jQuery mouseenter()事件示例2
让我们看看jQuery mouseenter()事件的另一个示例。
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").mouseenter(function(){
$("p").css("background-color", "lightgreen");
});
$("p").mouseleave(function(){
$("p").css("background-color", "yellow");
});
});
</script>
</head>
<body>
<p>Move your mouse cursor over this statement.</p>
</body>
</html>
输出: