1.I have a onclick event on,
我有一个onclick事件,
$('#locations').click(function(){
$('#train').unbind('click');
//do some stuff
}
2.Once the close button is clicked
2.单击关闭按钮
$('.close').click(function(){
//do some stuff
}
3.Then again if I click #train
3.再次点击#train
$('#train').bind('click', function() {
alert('train is clicked');
//do some stuff
}
Now the problem is #train is not firing.Is it to bind the event again on .close function?
现在问题是#train没有触发。它是否在.close函数上再次绑定事件?
Please suggest.Thanks in advance.
请建议。谢谢。
2 个解决方案
#1
34
Looking at your question, you dont seem to bind back the click
after you unbind
it, so it wont fire. (Assuming you've kept the sequence of your functionality right). You'll have to do it this way:
看看你的问题,你解开它之后似乎没有绑定点击,所以它不会开火。 (假设你保持了你的功能顺序)。你必须这样做:
//Set a function with the action you need to do after you click the train
function trainClick() {
alert('train is clicked');
//do some stuff
}
When you're unbinding, call unbind
with the function name:
当您解除绑定时,使用函数名称调用unbind:
$('#locations').click(function(){
$('#train').unbind('click',trainClick);
//do some stuff
}
Then, to bind the click (when #close
is clicked), you'd use :
然后,要绑定点击(单击#close时),您将使用:
$('.close').click(function(){
$('#train').bind('click',trainClick);
//do some stuff
}
NOTE :
注意 :
A better way would be use on
and off
, if youre using a version greater than jQuery v1.7 because, well.. it wont work. In the code above, just replace bind
with on
and unbind
with off
.
一个更好的方法是使用on和off,如果你使用的版本大于jQuery v1.7,因为,好吧..它不会工作。在上面的代码中,只需将bind替换为on,将unbind替换为off。
$('#train').on('click',trainClick);
$('#train').off('click',trainClick);
Hope this helps!
希望这可以帮助!
#2
4
BINDING AND UNBINDING HANDLERS
绑定和绑定处理器
The Key is Scope.
关键是范围。
You must declare and define the function (trainClick(){stuff it does})
outside the event-handler so that the other buttons' functions can see it.
你必须在事件处理程序之外声明和定义函数(trainClick(){stuff it does}),以便其他按钮的函数可以看到它。
Below is an example.
以下是一个例子。
function trainClick()
{
alert("train is clicked"); //Notice this function is declared outside event handlers below
}
$('#button1').on("click", trainClick); //this calls the above function
$("#button2").on("click",function(){
$("#button1").off("click",trainClick); //unbinds button1 from trainClick() function (event handler)
});//End Button 2 click
$("#button3").on("click",function(){
$("#button1").on("click",trainClick); //binds button1 to trainClick() function (event handler)
});//End Button 2 click
#1
34
Looking at your question, you dont seem to bind back the click
after you unbind
it, so it wont fire. (Assuming you've kept the sequence of your functionality right). You'll have to do it this way:
看看你的问题,你解开它之后似乎没有绑定点击,所以它不会开火。 (假设你保持了你的功能顺序)。你必须这样做:
//Set a function with the action you need to do after you click the train
function trainClick() {
alert('train is clicked');
//do some stuff
}
When you're unbinding, call unbind
with the function name:
当您解除绑定时,使用函数名称调用unbind:
$('#locations').click(function(){
$('#train').unbind('click',trainClick);
//do some stuff
}
Then, to bind the click (when #close
is clicked), you'd use :
然后,要绑定点击(单击#close时),您将使用:
$('.close').click(function(){
$('#train').bind('click',trainClick);
//do some stuff
}
NOTE :
注意 :
A better way would be use on
and off
, if youre using a version greater than jQuery v1.7 because, well.. it wont work. In the code above, just replace bind
with on
and unbind
with off
.
一个更好的方法是使用on和off,如果你使用的版本大于jQuery v1.7,因为,好吧..它不会工作。在上面的代码中,只需将bind替换为on,将unbind替换为off。
$('#train').on('click',trainClick);
$('#train').off('click',trainClick);
Hope this helps!
希望这可以帮助!
#2
4
BINDING AND UNBINDING HANDLERS
绑定和绑定处理器
The Key is Scope.
关键是范围。
You must declare and define the function (trainClick(){stuff it does})
outside the event-handler so that the other buttons' functions can see it.
你必须在事件处理程序之外声明和定义函数(trainClick(){stuff it does}),以便其他按钮的函数可以看到它。
Below is an example.
以下是一个例子。
function trainClick()
{
alert("train is clicked"); //Notice this function is declared outside event handlers below
}
$('#button1').on("click", trainClick); //this calls the above function
$("#button2").on("click",function(){
$("#button1").off("click",trainClick); //unbinds button1 from trainClick() function (event handler)
});//End Button 2 click
$("#button3").on("click",function(){
$("#button1").on("click",trainClick); //binds button1 to trainClick() function (event handler)
});//End Button 2 click