admin管理员组

文章数量:1391937

I am trying to use Coffeescript, but I am having a problem on my web application. Its methods are not called until I reload the page.

I think that what is missing is the $(document).ready(function () { part, but I couldn't find how to do it in the web.

file_name.js (works great)

$(document).ready(function () {
  $(document).on('click', '.add_fields', function(event) {
    event.preventDefault();
    /* Act on the event */
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
  });
});

file_name.coffee (doesn't work)

jQuery ->
  $('form').on 'click', '.add_fields', (event) ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()

How can I fix this?

I am trying to use Coffeescript, but I am having a problem on my web application. Its methods are not called until I reload the page.

I think that what is missing is the $(document).ready(function () { part, but I couldn't find how to do it in the web.

file_name.js (works great)

$(document).ready(function () {
  $(document).on('click', '.add_fields', function(event) {
    event.preventDefault();
    /* Act on the event */
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
  });
});

file_name.coffee (doesn't work)

jQuery ->
  $('form').on 'click', '.add_fields', (event) ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()

How can I fix this?

Share Improve this question asked May 20, 2018 at 1:09 Augusto CarmoAugusto Carmo 4,9642 gold badges37 silver badges79 bronze badges 1
  • 1 Coffeescript is just fancy Javascript. "On document ready" is not a feature of Javascript language, but feature of the browser or jQuery library. So, when in doubt, just write same Javascript code with block indentation instead of curly brackets, and replacing function() with -> – metalim Commented Jun 6, 2018 at 19:53
Add a ment  | 

1 Answer 1

Reset to default 6
$(document).ready ->

See example at https://github./jashkenas/coffeescript/blob/master/documentation/site/docs.coffee#L16

本文标签: javascriptCoffeescript equivalent to On Document ReadyStack Overflow