Jquery悬停不起作用

Par*_*eer 31 html javascript jquery javascript-events hover

我正在改变我的代码以与jQuery 1.8兼容,我坚持使用这个hover不起作用.当我使用同样的东西时,click它工作.这是我的代码,谁能告诉我哪里出错?

$(document).on('hover', '.top-level', function (event) {
  $(this).find('.actionfcnt').show();
  $(this).find('.dropfcnt').show();
}, function () {
  $(this).find('.dropfcnt').hide('blind', function () {
    $('.actionfcnt').hide();
  });
});
Run Code Online (Sandbox Code Playgroud)

nbr*_*oks 62

自jQuery 1.8开始不推荐使用:名称"hover"用作字符串"mouseenter mouseleave"的简写.它为这两个事件附加单个事件处理程序,并且处理程序必须检查event.type以确定事件是mouseenter还是mouseleave.不要将 "hover"伪事件名称与.hover()方法混淆,后者接受一个或两个函数.

资料来源:http://api.jquery.com/on/#additional-notes

这几乎说明了一切,你不能使用"悬停":

$(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();
    });
});
Run Code Online (Sandbox Code Playgroud)


Dzi*_*owy 8

没有"悬停"事件.有.hover()函数,需要2个回调(如你的例子).

  • 想象一下,如果所有的答案都可以如此简明扼要。很好! (2认同)

Dan*_*sky 5

.on函数只有 3 个参数:http : //api.jquery.com/on/

如果您不需要将处理程序绑定到动态添加的元素,那么您可以使用hover带有 2 个事件处理程序的旧函数。

$('.top-level').hover(function (event) { 
  $(this).find('.actionfcnt').show();
  $(this).find('.dropfcnt').show();
}, function (event) {   
  $(this).find('.dropfcnt').hide('blind', function(){
    $('.actionfcnt').hide();
  });
});?
Run Code Online (Sandbox Code Playgroud)

顺便说一下,$(selector).hover(handlerIn, handlerOut)是 的简写$(selector).mouseenter(handlerIn).mouseleave(handlerOut);

如果需要,请使用onformouseentermouseleaveevents:

$(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();
  });
});?
Run Code Online (Sandbox Code Playgroud)


Sud*_*oti 5

尝试:

$(".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();
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

或者

$(".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();
    });
  }
});
Run Code Online (Sandbox Code Playgroud)