jQuery代理插件 [英] jQuery delegates with plugins

查看:92
本文介绍了jQuery代理插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道是否可以使用带有插件的代理人?

jQuery代理是非常好的,特别是在使用表行点击事件时。 p>

例如,如果我将弹性插件附加到每个文本区域,我会这样做:

 code> $(textarea)。elastic(); 

但是,如何使用委托附加此插件?

解决方案

你可以欺骗。那么你可以在这种情况下。



你需要一个可以委托的事件,这个事件总是会在插件需要应用之前发生,并且有一些知道是否已经过去了您可以随时添加。



假设您正在关于此插件:



http://www.unwrongest.com/projects/elastic/



那么,因为你必须在打字/粘贴之前集中一个textarea,你可以这样做:

  if(!$(this).data('iselastic'))
$(this) iselastic',true).elastic();
})

我假设你至少运行jQuery 1.4。


jQuery delegates are great, especially when using with table row click events.

I was wondering if it's possible to use delegates with plug-ins as well?

For example if I attach elastic plug-in to every text area, I would do:

$("textarea").elastic();

But how would I attach this plug-in using delegate?

解决方案

You can cheat. Well, you can in this instance.

You need a delegate-able event that will always occur before the plugin needs to be applied and some means of knowing whether or not it already has been. You can always add that yourself though.

Assuming you are on about this plugin:

http://www.unwrongest.com/projects/elastic/

then, since you always have to focus a textarea before typing/pasting into it, you can do this:

$('textarea').live( 'focus', function(){
   if( !$(this).data('iselastic') )
      $(this).data('iselastic', true).elastic();
})

I'm assuming you're running jQuery 1.4 at least.

这篇关于jQuery代理插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆