为什么 .on() 方法不执行第一个处理程序
Why doesn't .on() method execute the first handler?
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')
在这种情况下我做错了什么?
您似乎将接受 2 个处理程序(一个用于mouseenter
,一个用于mouseleave
事件(hover
方法与 on
方法混淆了。您应该只将一个回调函数传递给 on
方法。在这种情况下,第一个函数用作可选的 data
参数,第二个函数用作处理程序:
.on( events [, selector ] [, data ], handler )
event
对象的data
属性引用传递的函数。您可以使用调用运算符调用该函数()
:
userList.on( "mouseover", "li", function(e) {
console.log('1');
x = setTimeout( function() {
doThis();
}, 700);
},function(event) {
event.data(); // calls the first function
// ...
});
另请注意,mouseenter
和mouseover
是两个不同的事件。你应该听mouseover/mouseout
或mouseenter/mouseleave
。
对于清除函数设置的超时setTimeout
您应该使用 clearTimeout
,clearInterval
用于清除setInterval
函数设置的间隔。
var x;
userList.on({
mouseenter: function() {
x = setTimeout( function() {
doThis();
}, 700);
},
mouseleave: function() {
clearTimeout(x);
// showProfile.css('display', 'none');
// ...
}
}, "li");
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 看起来第一个console.log是'It’不太对
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- 节点发布错误对象的第一个“属性”
- Javascript:表单验证getElementById仅返回第一个id元素
- 角度 2:使用一个 http 调用的结果来执行另一个调用
- 仅将 CSS 规则应用于
中的第一个 ,而不分配类/ID - 与 promise 一起使用时,异步瀑布不执行下一个回调方法
- 第一个javascript函数调用从未执行
- 在promise .race中第一个promise被解析(拒绝)后执行是否结束