我怎么能写这个coffeescript所以它不刷新我的页面?

时间:2022-02-18 00:40:05

This is my first Coffeescript function and can't figure out how to get this to not refresh my page after the user clicks and the event is fired:

这是我的第一个Coffeescript功能,无法弄清楚如何在用户点击并触发事件后不刷新我的页面:

jQuery ->
  $(".answer_link").click -> 
    $val = $(this).attr 'id'
    $id = $val.replace(/answer_link_/, '')
    $input = "#new_answer_" + $id
    $($input).toggle 'slow'

Thanks!

2 个解决方案

#1


23  

I think this is a question about jQuery or JavaScript. you can use .preventDefault() to do this:

我认为这是一个关于jQuery或JavaScript的问题。你可以使用.preventDefault()来做到这一点:

jQuery ->
  $(".answer_link").click (event)->

    #like this
    event.preventDefault()

    $val = $(this).attr 'id'
    $id = $val.replace(/answer_link_/, '')
    $input = "#new_answer_" + $id
    $($input).toggle 'slow'

more info about preventDefault.

有关preventDefault的更多信息。

#2


0  

Just add a return false at the end of the click function like this :

只需在点击功能的末尾添加一个返回false,如下所示:

jQuery ->
  $(".answer_link").click -> 
    $val = $(this).attr 'id'
    $id = $val.replace(/answer_link_/, '')
    $input = "#new_answer_" + $id
    $($input).toggle 'slow'
    return false

#1


23  

I think this is a question about jQuery or JavaScript. you can use .preventDefault() to do this:

我认为这是一个关于jQuery或JavaScript的问题。你可以使用.preventDefault()来做到这一点:

jQuery ->
  $(".answer_link").click (event)->

    #like this
    event.preventDefault()

    $val = $(this).attr 'id'
    $id = $val.replace(/answer_link_/, '')
    $input = "#new_answer_" + $id
    $($input).toggle 'slow'

more info about preventDefault.

有关preventDefault的更多信息。

#2


0  

Just add a return false at the end of the click function like this :

只需在点击功能的末尾添加一个返回false,如下所示:

jQuery ->
  $(".answer_link").click -> 
    $val = $(this).attr 'id'
    $id = $val.replace(/answer_link_/, '')
    $input = "#new_answer_" + $id
    $($input).toggle 'slow'
    return false