最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert jQuery .click into .live 'click' in CoffeeScript - Stack Overflow

programmeradmin1浏览0评论

This works:

$('#products.table tr').click ->
  $(@).toggleClass('row_selected')

This doesn't:

$('#products.table tr').live 'click', (event) ->
  $(@).toggleClass('row_selected')

Error in browser's console:

[Error] TypeError: 'undefined' is not a function (evaluating '$('#products.table tr').live')
    ready (products.js, line 10)
    fire (jquery.js, line 3049)
    fireWith (jquery.js, line 3161)
    ready (jquery.js, line 434)
    pleted (jquery.js, line 105)

What am I missing?

This works:

$('#products.table tr').click ->
  $(@).toggleClass('row_selected')

This doesn't:

$('#products.table tr').live 'click', (event) ->
  $(@).toggleClass('row_selected')

Error in browser's console:

[Error] TypeError: 'undefined' is not a function (evaluating '$('#products.table tr').live')
    ready (products.js, line 10)
    fire (jquery.js, line 3049)
    fireWith (jquery.js, line 3161)
    ready (jquery.js, line 434)
    pleted (jquery.js, line 105)

What am I missing?

Share Improve this question edited Sep 7, 2013 at 1:20 Meltemi asked Sep 7, 2013 at 1:10 MeltemiMeltemi 38.4k52 gold badges201 silver badges298 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

live has been deprecated as of jQuery version 1.7, and Rails 3's default jQuery version is 1.9. You need to use on instead:

Try:

$(document).on 'click', '#products.table tr', (event) ->
  if ( $(@).hasClass('row_selected') )
    $(@).removeClass('row_selected')
  else
    $(@).addClass('row_selected')

or, you could use the toggleClass method as suggested in the ments as:

$(document).on 'click', '#products.table tr', (event) ->
  $(@).toggleClass('row_selected')
发布评论

评论列表(0)

  1. 暂无评论