为什么.on()方法不执行第一个处理程序? [英] Why doesn't .on() method execute the first handler?

查看:95
本文介绍了为什么.on()方法不执行第一个处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var x;
userList.on( "mouseover", "li", function(e) {
    console.log('1');
    x = setTimeout( function() { 
        doThis();
    }, 700);
},function(){
    console.log('3');
    clearInterval(x);
});
userList.on( "mouseleave", "li", function(e) {
    showProfile.css('display', 'none');
});

现在,当我将鼠标悬停在用户列表上时,它会显示 console.log ('3')在这种情况下我做错了什么?

Now whenever i'm hovering over the userlist it shows me console.log('3') what i'm doing wrong in this case ?

推荐答案

好像你混淆悬停接受2个处理程序的方法(一个用于 mouseenter ,另一个用于 mouseleave event)方法上的。您应该只将一个回调函数传递给方法上的。在这种情况下,第一个函数用作可选的 data 参数,第二个函数用作处理程序:

It seems you are confusing hover method which accepts 2 handlers(one for mouseenter and one for the mouseleave event) with the on method. You should just pass one callback function to the on method. The first function in this case is used as the optional data parameter and the second function is used as the handler:

.on( events [, selector ] [, data ], handler )

data 事件的属性对象是指传递的函数。您可以使用()调用运算符调用该函数:

data property of the event object refers to the passed function. You can invoke the function using () invocation operator:

userList.on( "mouseover", "li", function(e) {
    console.log('1');
    x = setTimeout( function() { 
        doThis();
    }, 700);
},function(event) {
    event.data(); // calls the first function
    // ...
});

另请注意 mouseenter mouseover 是2个不同的事件。你应该听 mouseover / mouseout mouseenter / mouseleave

Also note that mouseenter and mouseover are 2 different events. You should either listen to mouseover/mouseout or mouseenter/mouseleave.

为了清除 setTimeout 函数设置的超时,你应该使用 clearTimeout clearInterval 用于清除由 setInterval 函数设置的间隔。

And for clearing timeouts set by setTimeout function you should use clearTimeout, clearInterval is used for clearing intervals set by setInterval function.

var x;
userList.on({
   mouseenter: function() {
       x = setTimeout( function() { 
          doThis();
       }, 700);
   },
   mouseleave: function() {
      clearTimeout(x);
      // showProfile.css('display', 'none');
      // ...
   }
}, "li");

这篇关于为什么.on()方法不执行第一个处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆