jQuery常用事件方法
-
jQuery事件方法與原生Js事件方法名稱類似,不需要寫on,通過jQuery物件打點呼叫,括號內引數是事件函式
-
mouseenter()方法:滑鼠進入一個元素觸發的事件
-
mouseleave()方法:滑鼠離開一個元素觸發的事件
-
注意:mouseenter和mouseleave沒有事件冒泡,在使用時替換mouseover和mouseout更加合適
下面是代碼對比:
<div >
<div ></div>
</div>
<script src="https://www.cnblogs.com/dreamtown/jq/jquery-1.12.4.min.js"></script>
<script>
var $box = $(".box");
var $parent = $(".parent");
//對比mouseenter、mouseleave 和 mouseover、mouseout
// 對比mouseenter、mouseleave 不冒泡
$box.mouseenter(function(){
console.log("box mouse in")
})
$box.mouseleave(function(){
console.log("box mouse out")
})
$parent.mouseenter(function(){
console.log("parent mouse in")
})
$parent.mouseleave(function(){
console.log("parent mouse out")
})

//mouseover、mouseout 冒泡
$box.mouseover(function(){
console.log("box mouse in")
})
$box.mouseout(function(){
console.log("box mouse out")
})
$parent.mouseover(function(){
console.log("parent mouse in")
})
$parent.mouseout(function(){
console.log("parent mouse out")
})

-
hover()方法:相當于將mouseenter和mouseleave事件進行了合寫
hover(滑鼠移上執行的事件函式,滑鼠離開執行的事件函式)
//hover() 對mouseenter和mouseleave合并書寫
//$box.hover(function () { }, function () { })
$box.hover(function(){
$box.addClass("big");
},function(){
$box.removeClass("big")
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/270858.html
標籤:jQuery
下一篇:jQuery其他關系查找方法
