Jquery 更多关于coffeescript的示例

Jquery 更多关于coffeescript的示例,jquery,coffeescript,toggle,Jquery,Coffeescript,Toggle,我正在尝试添加基本的多/少链接以切换元素可见性 如何插入preventDefault 而且它只在第一次切换时起作用,之后它不会切换出去。有错误吗 $("#description-link").click -> $("#project-description").show() $(this).text("Show description") , -> $("#project-description").hide() $(this).text("Hide descript

我正在尝试添加基本的多/少链接以切换元素可见性

如何插入
preventDefault

而且它只在第一次切换时起作用,之后它不会切换出去。有错误吗

$("#description-link").click ->
  $("#project-description").show()
  $(this).text("Show description")
, ->
  $("#project-description").hide()
  $(this).text("Hide description")
要访问preventDefault(),您需要将事件参数添加到函数中,并在该函数上调用preventDefault方法:

$("#description-link").click (event) ->
  event.preventDefault()
  ....

好吧,你可以试试这样:

$("#description-link").click (e) ->
  e.preventDefault()
  $('#project-description').toggle()
  a = $(this).text()
  a = if (a == 'Show Details') then 'Hide Details' else 'Show Details'
  $(this).text(a)
显然,
点击
(e)
之间需要空格。否则将抛出TypeError

编辑:刚刚意识到为什么需要空格…因为它是函数声明的一部分。我以前没把咖啡脚本弄糟过/掌纹