jquery.delegate() 和 Coffeescript

发布于 2024-12-15 12:08:24 字数 178 浏览 2 评论 0原文

我是 Coffeescript 的新手,想使用 jquery 1.6.4 中的 delegate() 方法。方法签名为.delegate(selector, eventType, handler)。我该如何在 Coffeescript 中调用这个方法?我正在使用 Rails 3.1。

谢谢, 丹妮.

I am a newbie in Coffeescript and would like to use the delegate() method in jquery 1.6.4. The method signature is .delegate( selector, eventType, handler ). How do I go about calling this method in Coffeescript? I am using Rails 3.1.

Thanks,
Dany.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

土豪我们做朋友吧 2024-12-22 12:08:24

就像咖啡脚本中的其他方法一样?

($ 'body').delegate '.external.link', 'click', (evt) ->
  # handler body

我觉得我对你的问题漏掉了一些东西......

just like any other method in coffeescript?

($ 'body').delegate '.external.link', 'click', (evt) ->
  # handler body

i feel like i'm missing something about your question...

静若繁花 2024-12-22 12:08:24

要回答您的 2 个处理程序问题:

$('body').hover ->
  # handler 1
  console.log 'in'
, ->
  # handler 2
  console.log 'out'

http://js2coffee.org/ 是一个很好的后台网站,如果您编写coffescript,无需自动编译器,允许您立即检查生成的javascript。

To reply to your 2 handlers question:

$('body').hover ->
  # handler 1
  console.log 'in'
, ->
  # handler 2
  console.log 'out'

http://js2coffee.org/ is a good website to have in background if you write coffeescript without an automatic compiler that allows you to check imediatly the resulting javascript.

傾旎 2024-12-22 12:08:24

这纯粹是美学问题,但我更喜欢将处理程序包装在命名函数中(如果有多个函数):

    over = => ...
    out  = => ...

    $el.hover over, out

对我来说,它看起来更干净。

It's a pure matter of aesthetics, but I prefer to wrap my handlers in named functions (if there's more than one):

    over = => ...
    out  = => ...

    $el.hover over, out

It just seems cleaner for me.

念三年u 2024-12-22 12:08:24

我可能很笨,但上面的正确答案花了我一些时间才弄清楚。
第一个参数是 div 或 ?您想要监视新元素,那么您需要提供元素标识符,然后提供事件类型

$('#forum-senate-admin-lists').delegate 'input[id=delete-forum-object]','click', (evt) ->
    deleteForumObject($(this))`

I may be dense but the correct answer above took me some time to figure out.
the first parameter is the div or ? that you want to watch for new elements, then you need to provide the element identifier , then the event type

$('#forum-senate-admin-lists').delegate 'input[id=delete-forum-object]','click', (evt) ->
    deleteForumObject($(this))`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文