jquery 解除绑定和绑定
我只想禁用用户在某些条件下单击某个元素,然后在另一个条件下重新绑定它的功能。以下是我正在使用的一些代码:
$('#navigation a').bind('click',function(e){
var $this = $(this);
var prev = current;
current = $this.parent().index() + 1;
if (current == 1){
$("#navigation a:eq(1)").unbind("click"); // remove the click for element
}
if (current >= 2){
$("#navigation a:eq(1)").bind("click"); // this doesn't work, but i want re-bind the click here.
} }
我需要做什么才能使其工作?
I just want to disable the ability for a user to click on an element for some condition and then re-bind it later for another condition. Here is some of the code I am working with:
$('#navigation a').bind('click',function(e){
var $this = $(this);
var prev = current;
current = $this.parent().index() + 1;
if (current == 1){
$("#navigation a:eq(1)").unbind("click"); // remove the click for element
}
if (current >= 2){
$("#navigation a:eq(1)").bind("click"); // this doesn't work, but i want re-bind the click here.
} }
What do I need to do to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您实际上只想禁用第一个导航链接的工作。如果是这种情况,您只需:
因为从事件处理程序返回
false
会阻止浏览器的默认操作(跟随链接)发生。不过,如果链接不是可点击的,则不要将其设为链接,而是将其变成
:(
假设链接的 class 属性是唯一有价值的属性复制)。
如果您确实希望取消绑定您编写的自定义处理程序,那么您需要为处理程序函数指定一个名称,以便可以再次引用它以进行重新绑定:
It sounds like you actually want to just disable the first navigation link from working. If that's the case, you simply want:
as returning
false
from an event handler prevents the browser's default action (of following the link) from occuring.Although, if the link is not meant to be clickable, don't make it a link, turn it into a
<span>
:(assuming that the class attribute of the link is the only worthy attribute to copy).
If you actually do wish to unbind the custom handler you wrote, then you want to give the handler function a name so it can be referenced again for rebinding: