我有一个区分单击和双击左键的脚本。成功检测到dbl click并且在事件时触发分配的功能,成功检测到单击,但在事件发生时无限重复分配的功能。 我已经更改了事件委托的原始脚本,因为我需要动态生成的dom元素来响应与这些事件相同的功能。在此更改后,单击会触发无限分配的功能。
//single and double clicks
jQuery.fn.single_double_click = function(single_click_callback, double_click_callback, timeout) {
return this.each(function(){
var clicks = 0, self = this;
//delegated one:
jQuery('#subunitscontainer').on('click', jQuery(this), function(event){
//non delegated one:
//single click has no problem but dynamically generated dom
//does not response to the event here.
//jQuery(this).click(function(event){
clicks++;
if (clicks == 1) {
setTimeout(function(){
if(clicks == 1) {
single_click_callback.call(self, event);
} else {
double_click_callback.call(self, event);
}
clicks = 0;
}, timeout || 300);
}
});
});
}
//end single and double clicks