简体   繁体   English

类更改后,JQuery单击事件无法正常工作

[英]JQuery click event not working correctly after class change

I have JQuery script similar to this: 我有类似这样的JQuery脚本:

$('.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");
});

My problem is that after I change the class the link is still refering to it's old class' click event. 我的问题是,在我更改类之后,链接仍然引用它的旧类'click事件。 Is there a way of rebinding? 有没有重新绑定的方法? Or am I doing this a bad way? 或者我这样做是不是很糟糕?

You can use this, both functions in 1: 你可以使用这两个函数1:

$('.unfollow_link, .follow_link').live("click", function(){
    $(this).toggleClass("follow_link unfollow_link");
});

See toggleClass , and the live function 请参阅toggleClass实时函数

.toggleClass( className ) classNameOne or more class names (separated by spaces) to be toggled for each element in the matched set. .toggleClass(className) className要为匹配集中的每个元素切换的一个或多个类名(由空格分隔)。

Edit for new jQuery versions 1.6.4+ 编辑新的jQuery版本1.6.4+

Since the live function does not exists anymore, here is a replacement: 由于live功能不再存在,这里有一个替代品:

$('body').on("click", '.unfollow_link, .follow_link', function(){
    $(this).toggleClass("follow_link unfollow_link");
});

The problem is that jQuery works by selecting elements (that's what $('.follow_link') does) and then binding an event handler to those elements (that's what .click*function(){ does). 问题是jQuery的工作原理是选择元素(这就是$('.follow_link')作用),然后将事件处理程序绑定到那些元素(这就是.click*function(){作用)。 It doesn't matter if the class changes. 如果班级改变也没关系。

Javascript has a feature called event bubbling. Javascript有一个名为event bubbling的功能。 This means that ancestor elements are notified of events on their descendants. 这意味着祖先元素会被告知其后代的事件。 This means that you can test selectors when the event occurs, rather than on document ready. 这意味着您可以在事件发生时测试选择器,而不是文档就绪。

jQuery makes this easy with the on function: 使用on函数,jQuery使这很简单:

$(document.body).on('click', '.follow_link', function(){
  // Do some stuff
  $(this).removeClass();
  $(this).addClass("unfollow_link");
}).on('click', '.unfollow_link', function(){
  // Do some stuff
  $(this).removeClass();
  $(this).addClass("follow_link");
});

Note that this function was introduced in jQuery 1.7. 请注意,此函数是在jQuery 1.7中引入的。 For compatibility with previous versions, use delegate . 为了与先前版本兼容,请使用delegate

Note: I just had this issue even if the on() function and still have the issue on JQuery 1.10, 注意:我刚刚遇到这个问题,即使on()函数仍然存在JQuery 1.10的问题,

Actually I resolved the problem by replacing: 实际上我通过替换解决了这个问题:

$('.unfollow_link, .follow_link').live("click", function(){
     $(this).toggleClass("follow_link unfollow_link");
});

By 通过

$(document).on('click','.unfollow_link, .follow_link', function(){
    $(this).toggleClass("follow_link unfollow_link");
});

It would be better to give each of the links a third class that designates that they are part of a group. 最好给每个链接指定一个第三类,指定它们是一个组的一部分。 Something like a_link : a_link这样的a_link

$('.a_link').bind('click', function () {
    $(this).toggleClass('follow_link unfollow_link');
});

Try: 尝试:

 
 
 
  
  $('.follow_link, .unfollow_link').click(function() { newclass = $(this).hasClass('follow_link') ? 'unfollow_link' : 'follow_link'; $(this).removeClass().addClass(newclass) });
 
  

edit: Niels' solution is much nicer. 编辑:尼尔斯的解决方案更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM