2012-09-04 21 views
24

Estoy cambiando mis códigos para ser compatible con jQuery 1.8 y estoy atascado con este hover que no funciona. Cuando usé lo mismo con un click funcionó. Aquí está mi código, ¿alguien puede decirme dónde me estoy equivocando?Jquery en vuelo estacionario no funciona

función
$(document).on('hover', '.top-level', function (event) { 
    $(this).find('.actionfcnt').show(); 
    $(this).find('.dropfcnt').show(); 
}, function() { 
    $(this).find('.dropfcnt').hide('blind', function() { 
    $('.actionfcnt').hide(); 
    }); 
}); 
+0

Lo que sucede el uso de "mouseover" en lugar de "flotar"? –

Respuesta

48

obsoleta a partir de jQuery 1.8: El nombre de "flotar" que se utiliza como una abreviatura de la cadena "mouseleave MouseEnter". Se adjunta un único controlador de eventos para esos dos eventos, y el controlador debe examinar event.type para determinar si el evento es mouseenter o mouseleave. No confunda el nombre de pseudo-evento "hover" con el método .hover(), que acepta una o dos funciones.

Fuente: http://api.jquery.com/on/#additional-notes

que más o menos lo dice todo, no puedes usar "flotar" para ello:

$(document).on('mouseenter','.top-level', function (event) { 
    $(this).find('.actionfcnt').show(); 
    $(this).find('.dropfcnt').show(); 
}).on('mouseleave','.top-level', function(){ 
    $(this).find('.dropfcnt').hide('blind', function(){ 
     $('.actionfcnt').hide(); 
    }); 
}); 
+0

supongamos que hay una llamada ajax y el contenido de .top-level se reemplaza por new entonces este también mouseenter funcionaría? –

+0

@param Sí, esto funcionará bien, esta sintaxis aún admite totalmente los eventos delegados (eventos activados por elementos que no estaban en DOM en el momento de la función def). Esto logrará lo que desea para los elementos generados dinámicamente. – nbrooks

+0

gracias por la respuesta :) simplemente lo comprobé usando contenido dinámico, funciona perfectamente –

2

.on sólo tiene 3 parámetros: http://api.jquery.com/on/

Si no necesita que sus manipuladores pueden unir a los elementos dinámicamente agregado así, entonces puede usar la buena función de edad hover con 2 controladores de eventos.

$('.top-level').hover(function (event) { 
    $(this).find('.actionfcnt').show(); 
    $(this).find('.dropfcnt').show(); 
}, function (event) { 
    $(this).find('.dropfcnt').hide('blind', function(){ 
    $('.actionfcnt').hide(); 
    }); 
});​ 

Por cierto, $(selector).hover(handlerIn, handlerOut) es la abreviatura de $(selector).mouseenter(handlerIn).mouseleave(handlerOut);.

Si es necesario, a continuación, utilizar on para mouseenter y mouseleave eventos:

$(document).on('mouseenter', '.top-level', function (event) { 
    $(this).find('.actionfcnt').show(); 
    $(this).find('.dropfcnt').show(); 
}).on('mouseleave', '.top-level', function (event) { 
    $(this).find('.dropfcnt').hide('blind', function(){ 
    $('.actionfcnt').hide(); 
    }); 
});​ 
+0

entonces no puedo usar el vuelo estacionario? en su lugar tengo que usar 'mouseenter' –

+0

gracias agradezco su respuesta y @nbrooks responde a los dos. ambos son correctos, gracias por tal explicación detallada –

5

no hay ningún evento "estacionario". hay función .hover() que toma 2 devoluciones de llamada (como en su ejemplo).

+1

Imagine si todas las respuestas pudieran ser tan concisas y directas al grano. ¡Gran trabajo! – Operator

0

Trate

$('.top-level').hover(function (event) { 
     $(this).find('.actionfcnt').show(); 
     $(this).find('.dropfcnt').show(); 
}, function(){ 
     $(this).find('.dropfcnt').hide('blind', function(){ 
      $('.actionfcnt').hide(); 
     }); 
}); 
0

probar este código con jQuery 1.8, creo que el trabajo bien:

$(document).ready(function(){ 
    $('.top-level').hover(
     function() { 
      $(this).find('.actionfcnt').show(); 
      $(this).find('.dropfcnt').show(); 
     }, 
     function() { 
      $(this).find('.dropfcnt').hide('blind', function(){ 
       $('.actionfcnt').hide(); 
      }); 
     } 
    ); 
}); 
1

Probar:

$(".top-level").on({ 
    mouseenter: function (event) { 
     $(this).find('.actionfcnt').show(); 
     $(this).find('.dropfcnt').show(); 
    }, 
    mouseleave: function (event) { 
     $(this).find('.dropfcnt').hide('blind', function(){ 
      $('.actionfcnt').hide(); 
     }); 
    } 
}); 

O

$(".top_level").on("hover", function(event) { 
    if(event.type == "mouseenter") { 
    $(this).find('.actionfcnt').show(); 
    $(this).find('.dropfcnt').show(); 
    } 
    else if (event.type == "mouseleave") { 
    $(this).find('.dropfcnt').hide('blind', function(){ 
     $('.actionfcnt').hide(); 
    }); 
    } 
}); 
Cuestiones relacionadas