jQuery - My first plugin, how to prevent duplicate application to an element
I have the following:
$('.comment_content').live('focus click', function() {
$(this).bTextAreaResizer();
});
I need it live on click because the comment_content boxes appear dynamically sometimes after page load. I just noticed that this is continuing to reapply the bTextAreaResizer() to the textarea each time which brings the br开发者_如何学Pythonowser to a stopping halt. In the plugin I have:
(function($){
$.fn.bTextAreaResizer = function(options) {
return this.each(function() {
//bind events
$(this).bind('scroll keyup', function() {
resizeTextArea($(this));
});
How can I say, only bind to this element if we haven't already? Is that possible?
Thanks
I guess there is a better way, but to avoid duplicate event binding, you could unbind the events before. You can use event namespaces for that. And btw, you don't need to loop over all the elements in this case:
return this.unbind('.btar')
.bind('scroll.btar keyup.btar', function() {
resizeTextArea($(this));
});
精彩评论