开发者

jquery live() method on custom functions

开发者 https://www.devze.com 2022-12-20 10:59 出处:网络
I have a custom event handler, or I suppose some call it a custom jquery function. My problem is I\'m trying to apply the live() method to it.But I\'m not too successful.

I have a custom event handler, or I suppose some call it a custom jquery function.

My problem is I'm trying to apply the live() method to it. But I'm not too successful.

Here's a simple custom jquery function:

$.fn.myFunction = function() { 
    return $(this).addClass('changed'); 
}

And here I use it:

$('.changePlease').myFunction();

Ok, simple enough. But how do I apply the live() method to it??

I actually have no idea if that's even possible. But I do use live() for other things, like:

$(".changePlease").live("click",function(){ alert("he开发者_如何学运维llo"); });

Any thoughts on this?


jquery.live works only for inbuilt events, and have not been adapted for custom events yet. You might need to find a workaround.


Does this work?

$.fn.myFunction = function() { 
  return $(this).live('click', function(){
    $(this).addClass('changed'); 
  });
};

Not sure what the use case here is. Sounds like you want to load new elements (via ajax or maybe by creating them with another event?), and once they get click events, to get the 'changed' class added to them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号