如何使jquery click事件仅在首次点击时触发 [英] How to make a jquery click event fire only on first click
问题描述
我有两个div(.basic1和.basic2).我希望.basic1在单击时淡出,而.basic2在我工作得很好的情况下淡出.唯一的问题是,一旦.basic2淡入,如果用户继续单击链接(.navbar1),它将在该div中一遍又一遍地淡入.我知道我需要使用.bind()函数,但是我似乎无法弄清楚代码中的放置位置.谢谢!
I have two divs (.basic1 and .basic2). I want .basic1 to fadeout on click, and .basic2 to fade in which I have working wonderfully. The only problem is that once .basic2 fades in, if the user continues to click the link (.navbar1), it will fade in this div over and over. I know that I need to use the .bind() function, but I can't seem to figure out where in my code to put it. Thanks!
$(document).ready(function() {
$('.navbar1').click(function(e){
e.preventDefault();
$('.basic2').hide().load('.basic2', function() {
$(this).delay(600).fadeIn(1000);
$('.basic1').fadeOut(1000);
});
});
});
推荐答案
使用 .one()
:
$('.navbar1').one('click', function(e) {
一旦触发click
事件,它将解除绑定.
It unbinds the click
event once you trigger it.
如果您不想双击某个元素,则应执行以下操作:
If you don't want an element to be double-clicked, something like this should work:
$(document).on('click', '.navbar1:not(.clicked)', function() {
// ...
});
要使.navbar1
不可点击,只需运行$('.navbar1').addClass('clicked')
.要使其可点击,请执行相反的操作.
To make .navbar1
unclickable, just run $('.navbar1').addClass('clicked')
. To make it clickable, do the opposite.
这篇关于如何使jquery click事件仅在首次点击时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!