在 onclick 事件内部,我绑定(bind)了另一个 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 - 如果有帮助的话:)
最佳答案
我猜是因为点击事件只是冒泡;-)
考虑以下情况:您有一堆分配给点击事件的事件处理程序,可能其中大多数您都不知道它们的存在
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
当您的第一个单击处理程序完成时,单击事件只会传递到队列中的下一个监听器(这称为冒泡),因为您不会阻止事件传播。这意味着您的点击处理程序
返回后,您会遇到以下情况:
New click handler <-- currently executing
Your click handler
...
System click handler 2
System click handler 1
关于javascript - 在点击事件中分配点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2170336/