JQuery停止从方法调用冒泡的事件
我试图了解jquery插件的结构,并编写了以下HTML代码,作为幻灯片的开始:JQuery停止从方法调用冒泡的事件,jquery,plugins,event-bubbling,Jquery,Plugins,Event Bubbling,我试图了解jquery插件的结构,并编写了以下HTML代码,作为幻灯片的开始: <div id="bubble" style="width: 200px; height: 200px; background: #ccc;"></div> <script type="text/javascript"> jQuery.noConflict(); (function($) { $(function() { $(document).ready(
<div id="bubble" style="width: 200px; height: 200px; background: #ccc;"></div>
<script type="text/javascript">
jQuery.noConflict();
(function($) {
$(function() {
$(document).ready(function() {
$("#bubble").bubble();
});
});
})(jQuery);
</script>
我不知道为什么,但单击气泡盒会调用next:method的多次迭代。有没有办法限制对next:方法的调用数量 您正在将
mousedown
和mouseup
绑定到next
函数,这将在每次单击时发送两个next
函数,我认为您无意这样做
要修复它,请删除其中一个绑定,或者只绑定
click
您正在绑定mousedown
和mouseup
到next
函数,这将在每次单击时发送两个next
函数,我认为您不是有意这么做的
要修复它,请删除其中一个绑定,或者只绑定单击也许您应该使用,以便只记录两次(因为您调用了next()两次):
fiddle:也许您应该使用,这样您只需记录两次(因为您调用next()两次):
fiddle:据他所说,鼠标点击由mousedown和mouseup事件组成,如果您同时绑定这两个事件,它将触发两次。jquery有“click”事件,这应该足够了。他说,一次鼠标点击包括一个mousedown和一个mouseup事件,如果你同时绑定这两个事件,它将触发两次。jquery有“click”事件,这应该足够了。只有绑定鼠标才能消除迭代问题。
(function($){
var i = 0;
var methods = {
mouseup: function(e){
$(this).bubble('next');
e.stopPropagation();
},
mousedown: function(e){
$(this).bubble('next');
e.stopPropagation();
},
next: function(){
console.log(i++);
}
};
$.fn.bubble = function(method){
$(this).bind("mouseup", methods.mouseup)
.bind("mousedown", methods.mousedown);
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
}
};
})(jQuery);
(function($){
var i = 0;
var methods = {
mouseup: function(e){
$(this).bubble('next');
e.stopImmediatePropagation();
},
mousedown: function(e){
$(this).bubble('next');
e.stopImmediatePropagation();
},
next: function(){
console.log(i++);
}
};
$.fn.bubble = function(method){
$(this).bind("mouseup", methods.mouseup)
.bind("mousedown", methods.mousedown);
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
}
};
})(jQuery);