jQuery-mouseleave上的setInterval和mouseenter上的clearInterval
我正在尝试动态地向jQuery-mouseleave上的setInterval和mouseenter上的clearInterval,jquery,function,setinterval,mouseleave,clearinterval,Jquery,Function,Setinterval,Mouseleave,Clearinterval,我正在尝试动态地向容器添加一些div(使用动态添加的函数)。当我点击这些DIV中的某个人时,菜单被插入到这个DIV。 在mouseleave上,我需要以delay或setInterval大约5秒的时间从DIV中删除菜单。这是可行的,但混乱。。。因此,我需要: 当mouseleve退出此DIV时,我立即将鼠标 回到这个DIV,我需要停止delay或setInterval。我需要停下来 删除菜单 函数'handler'有一些问题。因为当我添加 一些DIV到容器,这是我第一次需要使用 doublecl
容器添加一些div
(使用动态添加的函数)。当我点击这些DIV
中的某个人时,菜单
被插入到这个DIV
。
在mouseleave
上,我需要以delay
或setInterval
大约5秒的时间从DIV
中删除菜单。这是可行的,但混乱。。。因此,我需要:
当mouseleve
退出此DIV
时,我立即将鼠标
回到这个DIV
,我需要停止delay
或setInterval
。我需要停下来
删除菜单
函数'handler'
有一些问题。因为当我添加
一些DIV
到容器
,这是我第一次需要使用
doubleclik
,调用此函数。但我需要使用简单的点击
这里有一个例子:
HTML
什么是$(这个)
您没有使用或传递任何选择器
在
什么是$(这个)
您没有使用或传递任何选择器
使用$(“#容器”)。在上,这是一个解决方案:
JSFIDLE-工作示例:
jQuery
function handler() {
$(this).on({
click: function(e) {
clearTimeout(time);
if ($(this).find("#menu").length) {
return;
}
$('#menu').prependTo($(this));
$("#menu").css({
position: "absolute",
left: "100px"
}).show();
}
});
$(this).on({
mouseleave: function(e) {
time = setTimeout(function() {
$('#menu').appendTo('body').clearTimeout(time);;
}, 5000);
}
});
$(this).on({
mouseover: function(e) {
clearTimeout(time);
}
});
}
$("#add").on({
click: function(e) {
var timestamp = Date.now();
var posx = Math.floor(Math.random()*400);
var posy = Math.floor(Math.random()*400);
$('#container').append(function() {
return $('<div class="add_to_this" id="' + timestamp + '" style="left:'+posx+'px; top:'+posy+'px; ">Click here</div>').click(handler);
});
}
});
function handler1() {
clearTimeout(time);
if ($(this).find("#menu").length) {
return;
}
$('#menu').prependTo($(this));
$("#menu").css({
position: "absolute",
left: "100px"
}).show();
}
function handler2() {
time = setTimeout(function() {
$('#menu').appendTo('body').clearTimeout(time);;
}, 5000);
}
function handler3() {
clearTimeout(time);
}
$("#add").on({
click: function(e) {
var timestamp = Date.now();
var posx = Math.floor(Math.random() * 400);
var posy = Math.floor(Math.random() * 400);
$('#container').append(function() {
return $('<div class="add_to_this" id="' + timestamp + '" style="left:' + posx + 'px; top:' + posy + 'px; ">Click here</div>').click(handler1).mouseleave(handler2).mouseenter(handler3);
});
}
});
函数handler1(){
clearTimeout(时间);
if($(this.find(#menu”).length){
返回;
}
$('#menu')。prependTo($(this));
$(“#菜单”).css({
位置:“绝对”,
左:“100px”
}).show();
}
函数handler2(){
time=setTimeout(函数(){
$('#menu')。appendTo('body')。clearTimeout(time);;
}, 5000);
}
函数handler3(){
clearTimeout(时间);
}
$(“#添加”)。on({
点击:功能(e){
var timestamp=Date.now();
var posx=Math.floor(Math.random()*400);
var posy=Math.floor(Math.random()*400);
$(“#容器”).append(函数(){
返回$('Click here')。单击(handler1.mouseleave(handler2.mouseenter)(handler3);
});
}
});
这是一种解决方案:
JSFIDLE-工作示例:
jQuery
function handler() {
$(this).on({
click: function(e) {
clearTimeout(time);
if ($(this).find("#menu").length) {
return;
}
$('#menu').prependTo($(this));
$("#menu").css({
position: "absolute",
left: "100px"
}).show();
}
});
$(this).on({
mouseleave: function(e) {
time = setTimeout(function() {
$('#menu').appendTo('body').clearTimeout(time);;
}, 5000);
}
});
$(this).on({
mouseover: function(e) {
clearTimeout(time);
}
});
}
$("#add").on({
click: function(e) {
var timestamp = Date.now();
var posx = Math.floor(Math.random()*400);
var posy = Math.floor(Math.random()*400);
$('#container').append(function() {
return $('<div class="add_to_this" id="' + timestamp + '" style="left:'+posx+'px; top:'+posy+'px; ">Click here</div>').click(handler);
});
}
});
function handler1() {
clearTimeout(time);
if ($(this).find("#menu").length) {
return;
}
$('#menu').prependTo($(this));
$("#menu").css({
position: "absolute",
left: "100px"
}).show();
}
function handler2() {
time = setTimeout(function() {
$('#menu').appendTo('body').clearTimeout(time);;
}, 5000);
}
function handler3() {
clearTimeout(time);
}
$("#add").on({
click: function(e) {
var timestamp = Date.now();
var posx = Math.floor(Math.random() * 400);
var posy = Math.floor(Math.random() * 400);
$('#container').append(function() {
return $('<div class="add_to_this" id="' + timestamp + '" style="left:' + posx + 'px; top:' + posy + 'px; ">Click here</div>').click(handler1).mouseleave(handler2).mouseenter(handler3);
});
}
});
函数handler1(){
clearTimeout(时间);
if($(this.find(#menu”).length){
返回;
}
$('#menu')。prependTo($(this));
$(“#菜单”).css({
位置:“绝对”,
左:“100px”
}).show();
}
函数handler2(){
time=setTimeout(函数(){
$('#menu')。appendTo('body')。clearTimeout(time);;
}, 5000);
}
函数handler3(){
clearTimeout(时间);
}
$(“#添加”)。on({
点击:功能(e){
var timestamp=Date.now();
var posx=Math.floor(Math.random()*400);
var posy=Math.floor(Math.random()*400);
$(“#容器”).append(函数(){
返回$('Click here')。单击(handler1.mouseleave(handler2.mouseenter)(handler3);
});
}
});
$(“#容器”).append(function(){return$('Click here')。Click(handler);});如何添加这个函数?好的。我找到了。谢谢你的关注。我的英语不是很好,所以我花了很长时间才明白你的意思。。。再次使用thx.$('#container')。追加(函数(){return$('Click here')。单击(handler);});如何添加这个函数?好的。我找到了。谢谢你的关注。我的英语不是很好,所以我花了很长时间才明白你的意思。。。又是thx。
function handler1() {
clearTimeout(time);
if ($(this).find("#menu").length) {
return;
}
$('#menu').prependTo($(this));
$("#menu").css({
position: "absolute",
left: "100px"
}).show();
}
function handler2() {
time = setTimeout(function() {
$('#menu').appendTo('body').clearTimeout(time);;
}, 5000);
}
function handler3() {
clearTimeout(time);
}
$("#add").on({
click: function(e) {
var timestamp = Date.now();
var posx = Math.floor(Math.random() * 400);
var posy = Math.floor(Math.random() * 400);
$('#container').append(function() {
return $('<div class="add_to_this" id="' + timestamp + '" style="left:' + posx + 'px; top:' + posy + 'px; ">Click here</div>').click(handler1).mouseleave(handler2).mouseenter(handler3);
});
}
});