jquery - CoffeeScript 粗箭头访问不是父级 'this'

标签 jquery coffeescript arrow-functions

这很好用

    @nav.on 'click', ->
        _this.mover _this.nav.index $(@)

但我想知道是否可以像这样使用粗箭头

    @nav.on 'click', =>
        @mover @nav.index $(????)

但是我应该用什么来代替 @ 才能得到 this 而不是 _this 呢?

最佳答案

jQuery 事件处理程序获取一个事件对象作为参数,并且该事件对象具有 targetcurrentTarget属性:

@nav.on 'click', (ev) =>
    @mover @nav.index $(ev.currentTarget)

您可能想要 other properties of ev 之一根据您的具体情况。

关于jquery - CoffeeScript 粗箭头访问不是父级 'this',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10253494/

相关文章:

javascript - 输入 html 的输入值,而不是 dom

ruby-on-rails - 将 Coffeescript 实现到 Rails 应用程序中

javascript - Node 中的箭头函数提升?

JavaScript 箭头函数赋值

javascript - 如有必要,将 http 添加到链接

JQuery 字数统计和从 HTML 中剥离标签

javascript - CoffeeScript 中的 knockout 验证库

JavaScript ES6 : Test for arrow function, 内置函数,常规函数?

javascript - 在特定视口(viewport)宽度以下禁用事件处理程序

javascript - 如何初始化多个路由器?