类更改后,JQuery单击事件无法正常工作 [英] JQuery click event not working correctly after class change
本文介绍了类更改后,JQuery单击事件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类似的JQuery脚本:
I have JQuery script similar to this:
$('.follow_link').click(function(){
// Do some stuff
$(this).removeClass();
$(this).addClass("unfollow_link");
});
$('.unfollow_link').click(function(){
// Do some stuff
$(this).removeClass();
$(this).addClass("follow_link");
});
我的问题是,在我更改课程后,链接仍然引用它的旧课程'click事件。
有没有重新绑定的方法?或者我这样做是不是很糟糕?
My problem is that after I change the class the link is still refering to it's old class' click event. Is there a way of rebinding? Or am I doing this a bad way?
推荐答案
你可以使用它,这两个函数都在1:
You can use this, both functions in 1:
$('.unfollow_link, .follow_link').live("click", function(){
$(this).toggleClass("follow_link unfollow_link");
});
参见 toggleClass 和实时功能
.toggleClass(className)
className要为匹配集中的每个元素切换一个或多个类名(用空格分隔) 。
.toggleClass( className ) classNameOne or more class names (separated by spaces) to be toggled for each element in the matched set.
编辑新的jQuery版本1.6.4 +
由于 live
功能不再存在,这里有一个替代品:
Since the live
function does not exists anymore, here is a replacement:
$('body').on("click", '.unfollow_link, .follow_link', function(){
$(this).toggleClass("follow_link unfollow_link");
});
这篇关于类更改后,JQuery单击事件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文