admin管理员组

文章数量:1414604

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

本文标签: javascriptConvert jQuery click into live 39click39 in CoffeeScriptStack Overflow