【问题标题】:Div toggle in Rails 4 with coffeescript使用咖啡脚本在 Rails 4 中切换 Div
【发布时间】:2014-04-10 19:56:18
【问题描述】:

我有一个视图,其中对于一系列对象,我有几个包含详细信息的 div。对于每个 div,我都有一个 Toggle 链接。我的问题是 toggle() 仅适用于第一种 div,即注册的第一个点击事件。代码如下:

$(".class1").click (e) ->
    e.preventDefault()

    #get the index value I appended to this elements ID
    divIndex = $(this).attr("id").replace("link1_", "")

    #select and toggle the respective message
    $("#div1_" + divIndex).toggle()
return

$(".class2").click (e) ->
    e.preventDefault()

    #get the index value I appended to this elements ID
    divIndex = $(this).attr("id").replace("link2_", "")

    #select and toggle the respective message
    $("#div2_" + divIndex).toggle()
return

它只适用于注册的第一个点击事件。如果我更改顺序,另一个开始工作。对于第二个,它只是跟随视图中的链接。

有什么帮助吗?谢谢!

【问题讨论】:

    标签: jquery ruby-on-rails coffeescript


    【解决方案1】:

    我假设这段代码 sn-p 来自另一个函数体?尝试删除 return 语句。由于它们没有嵌套在您的咖啡脚本函数体中,它们指的是您在顶层拥有的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 2014-08-04
      • 1970-01-01
      相关资源
      最近更新 更多