【问题标题】:Fixing setTimeout in CoffeeScript修复 CoffeeScript 中的 setTimeout
【发布时间】:2018-01-11 20:49:05
【问题描述】:

我正在尝试将 this 代码 sn-p 从 JS 移植到 Coffee,但卡在忽略代码的设置超时函数上,超时时根本没有添加任何类:

$ ->

  $('.click').click ->
    $span = $(this).closest('span')
    $this = $(this)

    if $span.hasClass('fa-star')

      $this.removeClass 'active'

      setTimeout (->
        $this.removeClass 'active-2'
      ), 30

      $this.removeClass 'active-3'

      setTimeout (->
        $span.removeClass 'fa-star'
        $span.addClass 'fa-star-o'
      ), 15

    else

      $this.addClass 'active'
      $this.addClass 'active-2'

      setTimeout (->
        $span.addClass 'fa-star'
        $span.removeClass 'fa-star-o'
      ), 150

      setTimeout (->
        $this.addClass 'active-3'
      ), 150

尝试提取函数,但这也无济于事。

【问题讨论】:

    标签: javascript jquery coffeescript


    【解决方案1】:

    这个问题似乎只是与 jQuery 代码有关。

    这对于您拥有的 html 来说看起来是错误的。

    $span = $(this).closest('span')
    

    改成

    $span = $(this).children().first()
    

    DEMO

    【讨论】:

    • 很遗憾,没有效果
    • @TheWhizofOz 问题似乎出在 jQuery 代码上。
    • $(this).find('.fa')$(this).find('span.fa') 可能是更可靠的选择。不过可能没那么重要。
    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 2012-11-15
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2013-06-25
    相关资源
    最近更新 更多