使用coffeescript在Rails 4中进行Div切换

时间:2014-04-10 10:09:20

标签: jquery ruby-on-rails coffeescript

我有一个视图,其中,对于一系列对象,我有几个包含详细信息的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

它仅适用于注册的第一个单击事件。如果我改变订单,另一个开始工作。对于第二个,它只是在视图中的链接。

请帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:1)

我认为这段代码片段来自另一个函数体?尝试删除return语句。由于它们没有嵌套在您的咖啡脚本功能体中,因此它们指的是您在顶层的功能。