Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

Does jQuery have a handleout for .delegate('hover')?

I am trying to use:

$('mydiv').delegate('hover', function() {  
    $('seconddiv').show();  
}, function() {  
    //For some reason jQuery won't run this line of code  
    $('seconddiv').hide();  
});
question from:https://stackoverflow.com/questions/4772287/does-jquery-have-a-handleout-for-delegatehover

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

User113716's great answer will no longer work in jQuery 1.9+, because the pseudo-event hover is no longer supported (upgrade guide).

Also since jQuery 3.0 delegate() for binding events is officially deprecated, so please use the new on()(docs) for all event binding purposes.

You can easily migrate user113716's solution by replacing hover with mouseenter mouseleave and switching to the on() syntax:

$('mydiv').on('mouseenter mouseleave', 'seconddiv', function(event) {
    $(this).toggle( event.type === 'mouseenter' );  
});

If your problem is more complex than a simple toggle, I suggest binding two separate events:

$('mydiv').on('mouseenter', 'seconddiv', function( event ) {
    // do something
}).on('mouseleave', 'seconddiv', function( event ) {
    // do something different
});

NB: Since hover was removed in v1.9 and on() was introduced in v1.7, there is no real need for a solution using delegate() - but if you like it for some reason; it is still there (for now) and does the job:

$('mydiv').delegate('seconddiv','mouseenter mouseleave', function(event) {
    $(this).toggle( event.type === 'mouseenter' );  
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...