• <tfoot id='pXtuH'></tfoot>

    <i id='pXtuH'><tr id='pXtuH'><dt id='pXtuH'><q id='pXtuH'><span id='pXtuH'><b id='pXtuH'><form id='pXtuH'><ins id='pXtuH'></ins><ul id='pXtuH'></ul><sub id='pXtuH'></sub></form><legend id='pXtuH'></legend><bdo id='pXtuH'><pre id='pXtuH'><center id='pXtuH'></center></pre></bdo></b><th id='pXtuH'></th></span></q></dt></tr></i><div id='pXtuH'><tfoot id='pXtuH'></tfoot><dl id='pXtuH'><fieldset id='pXtuH'></fieldset></dl></div>
  • <legend id='pXtuH'><style id='pXtuH'><dir id='pXtuH'><q id='pXtuH'></q></dir></style></legend>
  • <small id='pXtuH'></small><noframes id='pXtuH'>

      <bdo id='pXtuH'></bdo><ul id='pXtuH'></ul>

      1. 我可以从咖啡脚本文件中调用 Javascript 函数吗?

        时间:2024-04-19

        <legend id='dDMlm'><style id='dDMlm'><dir id='dDMlm'><q id='dDMlm'></q></dir></style></legend>

          <bdo id='dDMlm'></bdo><ul id='dDMlm'></ul>

          1. <small id='dDMlm'></small><noframes id='dDMlm'>

              <tbody id='dDMlm'></tbody>

                <i id='dDMlm'><tr id='dDMlm'><dt id='dDMlm'><q id='dDMlm'><span id='dDMlm'><b id='dDMlm'><form id='dDMlm'><ins id='dDMlm'></ins><ul id='dDMlm'></ul><sub id='dDMlm'></sub></form><legend id='dDMlm'></legend><bdo id='dDMlm'><pre id='dDMlm'><center id='dDMlm'></center></pre></bdo></b><th id='dDMlm'></th></span></q></dt></tr></i><div id='dDMlm'><tfoot id='dDMlm'></tfoot><dl id='dDMlm'><fieldset id='dDMlm'></fieldset></dl></div>
                • <tfoot id='dDMlm'></tfoot>
                  本文介绍了我可以从咖啡脚本文件中调用 Javascript 函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从 Coffeescript 文件调用一个 JavaScript 函数,用于我的 $(document).ready() 并且它永远不会被调用.

                  I'm trying to call a javascript function from a Coffeescript file, for my $(document).ready() and it's never getting called.

                  我要调用的函数来自一个外部源,它包含在我的 html 头元素中,就在我的咖啡脚本文件的包含之前,如下所示:

                  The function I want to call comes from an external source that I have included in my html head element, just before the include of my coffeescript file, like this:

                  <script src="external.js"></script>
                  <%= javascript_include_tag params[:controller], 'data-turbolinks-track' => true %>
                  

                  在我的咖啡脚本文件(称为 someControllerName.coffee)中,我这样做:

                  and in my coffeescript file (called someControllerName.coffee) I do this:

                  ready = ->
                    ... call my method
                  
                  $ -> ready
                  

                  这是正确的方法吗?我可以在 Chrome 中看到我的脚本已编译为 javascript,我可以在检查器的网络"选项卡中看到它.

                  Is this the correct way? I can see in Chrome that my script is compile to javascript, and I can see it in the Network tab of the inspector.

                  我使用的是 Rails 4,这是我的 application.js

                  I'm using Rails 4, and this is my application.js

                  //= require jquery
                  //= require jquery.turbolinks
                  //= require jquery_ujs
                  //= require bootstrap
                  //= require turbolinks
                  //= require_tree .
                  

                  如果我用 alert("..."); 替换我的方法调用,它会起作用,如果我在 $(document).ready() 中使用 javascript 调用我的 javascript 方法 效果很好.

                  If I replace my method call by alert("..."); it works, and if I call my javascript method using javascript in $(document).ready() it works fine.

                  这是我真正的 javascript 函数:

                  Here's my real javascript function:

                  var ready = function () {
                    $('pre code').each(function (i, e) {
                        hljs.highlightBlock(e)
                    });
                  };
                  
                  $(document).ready(ready);
                  

                  我解决了这个问题:

                  highlight = ->
                    $("pre code").each (i, e) ->
                      hljs.highlightBlock(e)
                  
                  $(document).on 'ready page:load', ->
                    highlight()
                  

                  推荐答案

                  这和你想的不一样:

                  $ -> ready
                  

                  在 JavaScript 中是:

                  In JavaScript that is:

                  $(function() {
                      return ready;
                  });
                  

                  您的问题是 ready 只是对函数的引用,它不像在 Ruby 中那样是函数调用.您必须说 ready() 才能调用该函数,函数调用括号仅在您使用参数调用函数时是可选的:fxf(x) 相同,但 ff() 不同.

                  Your problem is that just ready is simply a reference to the function, it is not a function call like it would be in Ruby. You'd have to say ready() to call the function, the function calling parentheses are only optional when you're calling a function with arguments: f x and f(x) are the same but f and f() are different.

                  我想你想说:

                  $ -> ready()
                  

                  甚至:

                  $ ready # same as $(ready)
                  

                  这篇关于我可以从咖啡脚本文件中调用 Javascript 函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Node.js:文档返回未定义 - Mongoose 下一篇:我的 JavaScript 模式/实践很糟糕.我应该去哪里寻求帮助?

                  相关文章

                    <bdo id='ZpFiF'></bdo><ul id='ZpFiF'></ul>
                  <i id='ZpFiF'><tr id='ZpFiF'><dt id='ZpFiF'><q id='ZpFiF'><span id='ZpFiF'><b id='ZpFiF'><form id='ZpFiF'><ins id='ZpFiF'></ins><ul id='ZpFiF'></ul><sub id='ZpFiF'></sub></form><legend id='ZpFiF'></legend><bdo id='ZpFiF'><pre id='ZpFiF'><center id='ZpFiF'></center></pre></bdo></b><th id='ZpFiF'></th></span></q></dt></tr></i><div id='ZpFiF'><tfoot id='ZpFiF'></tfoot><dl id='ZpFiF'><fieldset id='ZpFiF'></fieldset></dl></div>

                  <tfoot id='ZpFiF'></tfoot>

                    <legend id='ZpFiF'><style id='ZpFiF'><dir id='ZpFiF'><q id='ZpFiF'></q></dir></style></legend>

                    1. <small id='ZpFiF'></small><noframes id='ZpFiF'>