[英]off hover jquery using .on()
我知道查询中的悬停方法允许您指定当用户悬停时发生的事情以及当用户不悬停时会发生什么。 但是,我使用.on()来处理悬停事件,因为内容是动态创建的。 当用户不悬停时,如何将其恢复到原始状态。 这是我的代码,我尝试过.off()但它没有给出我正在寻找的结果:
$('tr').on('hover', 'td', function(){
$(this).fadeTo(500, 1 )
})
这是我尝试过的:
$('tr').off('hover', 'td', function(){
$(this).fadeTo(500, .85 )
})
谢谢。
如果要使用.on()
,则处理程序的事件是“mouseenter”和“mouseleave”。 你可以通过一个电话完成:
$('tr').on({
mouseenter: function() {
$(this).fadeTo(500, 1); // or whatever
},
mouseleave: function() {
$(this).fadeTo(500, 0.85); // or whatever
}
}, 'td');
您也可以使用CSS:“hover”伪类来完成此操作。 在某种程度上,即使在IE的旧版本中也是如此。 您也可以为更改设置动画。
这就是你需要的
$('tr').on('mouseenter', 'td', function(){
$(this).fadeTo(500, 1 )
}).on('mouseleave', 'td', function(){
$(this).fadeTo(500, .85 )
})
你可以用纯CSS做到这一点但是你去了:
$('tr').on('mouseenter mouseleave', 'td', function( e ){
$(this).fadeTo(500, e.type=="mouseenter" ? 1 : 0.85 );
});
使用悬停:
$('tr td').hover(function( e ){
$(this).fadeTo(500, e.type=="mouseenter" ? 1 : 0.85 );
});
小费:
.on('hover'
不会像使用方法引用$(selector).hover(handlerIn, handlerOut)
那样单独绑定对mouseenter mouseleave
事件的直接引用,而只是hover
事件。
恢复:
$('tr').on('hover', 'td', function( e ){
// no separated "mouseenter" and no "mouseleave" e.type reference here :(
// just "hover" event
});
$('tr').on('mouseenter mouseleave', 'td', function( e ){
// e.type are defined :)
});
$('tr').on('mouseenter', 'td', function( e ){
// function only for 'mouseenter' event
}).on('mouseleave', 'td', function(){
// function only for 'mouseleave' event
});
$('tr td').hover(function( e ){
// e.type "mouseenter" and "mouseleave" are in event reference :)
});
// $("tr td").hover(handlerIn, handlerOut)
$('tr td').hover(function(){
// Method default // e.type reference == "mouseenter"
}, function(){
// Method default // e.type reference == "mouseleave"
});
现在,它取决于您是否需要使用.on()
(动态创建的元素)将事件委托给元素,或者仅适合您需要的.hover()
。
关于.off()
方法,你可以仔细看看它的作用: 这里
基本上,如果在某些时候你想删除任何进一步的事件委托给一个元素而不是你使用.off():
$('#selector').on('click', 'button', function(){
// Function callback:
alert('I will alert only once cause of off()');
$('#selector').off('click', 'button');
});
悬停不是一个事件,它是mouseenter
和mouseleave
事件处理程序的快捷方式
$('tr').on('mouseenter', 'td', function(){
$(this).fadeTo(500, 1 )
}).on('mouseleave', 'td', function(){
$(this).fadeTo(500, .85 )
})
$('.element').hover(
function () {
$(this).fadeTo(500, 1);
},
function () {
$(this).fadeTo(500, .85);
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.