简体   繁体   English

Coffeescript相当于On Document Ready

[英]Coffeescript equivalent to On Document Ready

I am trying to use Coffeescript, but I am having a problem on my web application. 我正在尝试使用Coffeescript,但我的Web应用程序出现问题。 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. 我认为缺少的是$(document).ready(function () { part,但我找不到如何在网上做到这一点。

file_name.js (works great) file_name.js (效果很好)

$(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) file_name.coffee (不起作用)

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? 我怎样才能解决这个问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM