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

前端 未结 5 1368
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 03:58

I am trying to use:

$(\'mydiv\').delegate(\'hover\', function() {  
    $(\'seconddiv\').show();  
}, function() {  
    //For some reason jQuery won\'t ru         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 04:17

    With delegate()(docs) , you assign it to a container, and the first argument is the element that should trigger the event.

    Also, .delegate() accepts only one function, so for the hover event you need to test the event.type to determine show()(docs) or hide()(docs) .

    $('.someContainer').delegate('.someTarget','hover', function( event ) {
        if( event.type === 'mouseenter' )  
            $('seconddiv').show();  
        else
            $('seconddiv').hide();  
    });
    

    For show/hide, a shorter way to write this is to use toggle()(docs), which can accept a switch argument where true means show and false means hide:

    $('.someContainer').delegate('.someTarget','hover', function( event ) {
        $('seconddiv').toggle( event.type === 'mouseenter' );  
    });
    

    Note that the mouseenter event is reported as of jQuery 1.4.3. If you're using 1.4.2, it will be reported as mouseover.


    EDIT:

    If I'm understanding your comments below, you'd have something like this (using the proper selectors of course).

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

提交回复
热议问题