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

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

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')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745193763a4615990.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信