我知道查询中的悬停方法允许您指定当用户悬停时发生的事情以及当用户不悬停时会发生什么.但是,我使用.on()来处理悬停事件,因为内容是动态创建的.当用户不悬停时,如何将其恢复到原始状态.这是我的代码,我尝试过.off()但它没有给出我正在寻找的结果:
$('tr').on('hover', 'td', function(){
$(this).fadeTo(500, 1 )
})
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
$('tr').off('hover', 'td', function(){
$(this).fadeTo(500, .85 )
})
Run Code Online (Sandbox Code Playgroud)
谢谢.
Poi*_*nty 11
如果要使用.on(),处理程序的事件是"mouseenter"和"mouseleave".你可以通过一个电话完成:
$('tr').on({
mouseenter: function() {
$(this).fadeTo(500, 1); // or whatever
},
mouseleave: function() {
$(this).fadeTo(500, 0.85); // or whatever
}
}, 'td');
Run Code Online (Sandbox Code Playgroud)
您也可以使用CSS:"hover"伪类来完成此操作.在某种程度上,即使在IE的旧版本中也是如此.您也可以为更改设置动画.
Opt*_*ime 11
这就是你需要的
$('tr').on('mouseenter', 'td', function(){
$(this).fadeTo(500, 1 )
}).on('mouseleave', 'td', function(){
$(this).fadeTo(500, .85 )
})
Run Code Online (Sandbox Code Playgroud)
你可以用纯 CSS来完成,但你可以这样做:
$('tr').on('mouseenter mouseleave', 'td', function( e ){
$(this).fadeTo(500, e.type=="mouseenter" ? 1 : 0.85 );
});
Run Code Online (Sandbox Code Playgroud)
使用悬停:
$('tr td').hover(function( e ){
$(this).fadeTo(500, e.type=="mouseenter" ? 1 : 0.85 );
});
Run Code Online (Sandbox Code Playgroud)
提示:
.on('hover'不会mouseenter mouseleave像使用方法引用那样单独绑定对事件的直接引用$(selector).hover(handlerIn, handlerOut),而只是绑定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"
});
Run Code Online (Sandbox Code Playgroud)
现在这取决于您是否需要使用.on()(动态创建的元素)将事件委托给元素,或者是否.hover()适合您的需要。
关于.off()方法,您可以仔细查看它的作用:这里
基本上,如果在某些时候您想删除对元素的任何进一步事件委托,而不是使用 .off():
$('#selector').on('click', 'button', function(){
// Function callback:
alert('I will alert only once cause of off()');
$('#selector').off('click', 'button');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8940 次 |
| 最近记录: |