在点击事件中指定点击事件(Assign click event inside click event)

在onclick事件中,我绑定了另一个onclick事件。 但是当我启动第一个事件时,第二个事件总是执行:

var MiniMenu = { show : function(menu_id, element){ // this doesn't have any thing to do with the problem - I think position = $(element).offset(); $('#' + menu_id).css({ left : position.left, top : position.top + 13 }).show(); // Why is this event called on the first click, // even though it isn't bound at that time? $(document).click(function(){ alert('here') $('.mini-menu').hide(); $(document).unbind('click') }) } }

我正在使用Adobe Air - 如果有帮助的话:)

Inside of an onclick event, I'm binding another onclick event. But when I initiate the first event, the second always executes:

var MiniMenu = { show : function(menu_id, element){ // this doesn't have any thing to do with the problem - I think position = $(element).offset(); $('#' + menu_id).css({ left : position.left, top : position.top + 13 }).show(); // Why is this event called on the first click, // even though it isn't bound at that time? $(document).click(function(){ alert('here') $('.mini-menu').hide(); $(document).unbind('click') }) } }

I'm using Adobe Air - if that helps :)

最满意答案

我猜是因为点击事件只是泡了起来;-)

请考虑以下事项:您有一堆事件处理程序分配给click事件,可能大多数事件处理程序不知道它们的存在

Your click handler <-- currently executing ... System click handler 2 <-- already finished System click handler 1

现在,执行时,您的点击处理程序将为此事件添加另一个侦听器。

New click handler Your click handler <-- currently executing ... System click handler 2 System click handler 1

当您的第一个点击处理程序完成后,点击事件会传递给队列中的下一个侦听器(这称为冒泡),因为您不阻止事件传播。 这意味着在Your click handler返回后,您有以下情况:

New click handler <-- currently executing Your click handler ... System click handler 2 System click handler 1

I guess because the click-event just bubbles up ;-)

Think of the following: you have a stack of event handlers assigned to the click event, probably most of them without you knowing of their existance

Your click handler <-- currently executing ... System click handler 2 <-- already finished System click handler 1

Now while executing, your click handler adds another listener to this event.

New click handler Your click handler <-- currently executing ... System click handler 2 System click handler 1

When your first click handler finishes, the click event just gets passed to the next listener in the queue (this is called bubbling), because you don't prevent the event propagation. That means that after Your click handler returns, you have the following situation:

New click handler <-- currently executing Your click handler ... System click handler 2 System click handler 1在点击事件中指定点击事件(Assign click event inside click event)

在onclick事件中,我绑定了另一个onclick事件。 但是当我启动第一个事件时,第二个事件总是执行:

var MiniMenu = { show : function(menu_id, element){ // this doesn't have any thing to do with the problem - I think position = $(element).offset(); $('#' + menu_id).css({ left : position.left, top : position.top + 13 }).show(); // Why is this event called on the first click, // even though it isn't bound at that time? $(document).click(function(){ alert('here') $('.mini-menu').hide(); $(document).unbind('click') }) } }

我正在使用Adobe Air - 如果有帮助的话:)

Inside of an onclick event, I'm binding another onclick event. But when I initiate the first event, the second always executes:

var MiniMenu = { show : function(menu_id, element){ // this doesn't have any thing to do with the problem - I think position = $(element).offset(); $('#' + menu_id).css({ left : position.left, top : position.top + 13 }).show(); // Why is this event called on the first click, // even though it isn't bound at that time? $(document).click(function(){ alert('here') $('.mini-menu').hide(); $(document).unbind('click') }) } }

I'm using Adobe Air - if that helps :)

最满意答案

我猜是因为点击事件只是泡了起来;-)

请考虑以下事项:您有一堆事件处理程序分配给click事件,可能大多数事件处理程序不知道它们的存在

Your click handler <-- currently executing ... System click handler 2 <-- already finished System click handler 1

现在,执行时,您的点击处理程序将为此事件添加另一个侦听器。

New click handler Your click handler <-- currently executing ... System click handler 2 System click handler 1

当您的第一个点击处理程序完成后,点击事件会传递给队列中的下一个侦听器(这称为冒泡),因为您不阻止事件传播。 这意味着在Your click handler返回后,您有以下情况:

New click handler <-- currently executing Your click handler ... System click handler 2 System click handler 1

I guess because the click-event just bubbles up ;-)

Think of the following: you have a stack of event handlers assigned to the click event, probably most of them without you knowing of their existance

Your click handler <-- currently executing ... System click handler 2 <-- already finished System click handler 1

Now while executing, your click handler adds another listener to this event.

New click handler Your click handler <-- currently executing ... System click handler 2 System click handler 1

When your first click handler finishes, the click event just gets passed to the next listener in the queue (this is called bubbling), because you don't prevent the event propagation. That means that after Your click handler returns, you have the following situation:

New click handler <-- currently executing Your click handler ... System click handler 2 System click handler 1