Javascript 如何撤消jQuery';是一个()在()上使用吗?
我有一个单击事件,需要触发的次数不超过一次:Javascript 如何撤消jQuery';是一个()在()上使用吗?,javascript,jquery,Javascript,Jquery,我有一个单击事件,需要触发的次数不超过一次: $(".menuIco").one('click', function() { $(this).menuIcoClick($(this)); }); …这将导致功能: $.fn.menuIcoClick = function(menuIco) { [...does some animations, not important...] } 然而,在其他事件之后,我希望该事件能够再次启动。我正在尝试使
$(".menuIco").one('click', function() {
$(this).menuIcoClick($(this));
});
…这将导致功能:
$.fn.menuIcoClick = function(menuIco) {
[...does some animations, not important...]
}
然而,在其他事件之后,我希望该事件能够再次启动。我正在尝试使用on()
再次使用menuIcoClick
函数绑定menuIco
的单击事件:
在
但是它不起作用,
on()
方法不允许我再次单击该元素(并且只触发一次事件),正如我预期的那样。您可以在第一个函数中使用.off
:
$(".menuIco").on('click', function() {
$(this).menuIcoClick($(this));
$(".menuIco").off('click'); // Here we disable the event, on the first execution
});
要再次执行该事件,需要将其设置为一个新函数,并附加到所需的事件
如果这不起作用,请向我们发送一个JSFIDLE,显示您的问题的更多细节。仅供参考,您不需要将元素作为参数传递给您的插件,因为它将通过
this
关键字提供。我很想这样做,但在一个简单的事件上却无法工作。无论如何,问题是我不能绑定
(或打开函数()。我不知道为什么这不起作用:$(.menuIco”).on('click',$(.menuIco”).menuIcoClick()在('click',function(){$(this.menuIcoClick();})上
我现在知道问题出在哪里了,我应该写$(“.menuIco”).on('click',$(“.menuIco”).menuIcoClick)代码>,而不是(…).menuIcoClick()
@OverdrivenFall我做了一把小提琴,上面显示了我想解释的内容:我在
上使用了另一个sintax。
$("#icoBack").click(function() {
$('#analiza').animate({left: ($("#analiza").data("left")), top: ($("#analiza").data("top"))}, 0).on('click', $('#analiza').menuIcoClick($('#analiza')));
});
$(".menuIco").on('click', function() {
$(this).menuIcoClick($(this));
$(".menuIco").off('click'); // Here we disable the event, on the first execution
});