实例 触发所选元素的 mouseleave 事件

x
 
<!DOCTYPE html>
<html>
<head>
<script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p").mouseenter(function(){
    $("p").css("background-color", "yellow");
  });
  $("p").mouseleave(function(){
    $("p").css("background-color", "lightgray");
  });
  $("#btn1").click(function(){
    $("p").mouseenter();
  });  
  $("#btn2").click(function(){
    $("p").mouseleave();
  }); 
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button id="btn1">Trigger mouseenter event</button><br>
<button id="btn2">Trigger mouseleave event</button>
</body>
</html>
                    

输出结果