【问题标题】:How to enqueue one script after another in WordPress `functions.php`, and have them both work together?如何在 WordPress `functions.php` 中将一个又一个脚本排入队列,并让它们一起工作?
【发布时间】:2019-01-26 10:20:36
【问题描述】:

我有 2 个.js 文件,第一个是plugins.js,第二个是ui.js,两者都在检查器的“调试”中可见,因此都包含在内。

我在 plugins.js 中有一个 jquery 函数,我需要在 ui.js 中调用它,所以我首先将 plugins.js 加入队列,然后紧接着在 ui.js 加入队列。

但是,网络浏览器中的控制台告诉我该函数未定义,并且它不起作用。

如果我将函数从plugins.js 剪切并粘贴到ui.js 中,它会起作用。当我在 JSfiddle 中尝试它时它也有效。

functions.php

function add_scripts() { 

wp_enqueue_script( 'plugins', get_template_directory_uri() . '/javascript/plugins.js', array( 'jquery', 'jquery-ui-core' )           , '1.0.0', true );
wp_enqueue_script( 'ui'     , get_template_directory_uri() . '/javascript/ui.js'     , array(), '1.0.0', true );
}
add_action('wp_enqueue_scripts', 'add_scripts');

plugins.js:

jQuery(document).ready(function($) {
    function PNGPreloader(e,t,i,r,n,a){...}
});

ui.js:

jQuery(document).ready(function($) {
    $(...).each(function(){
        mobileIcons[ID] = new PNGPreloader($object,frames,size[0],size[1],20,false);
    });

如果我将plugins.js 中的函数放在ui.js 中,它会起作用:

jQuery(document).ready(function($) {
    function PNGPreloader(e,t,i,r,n,a){...}
    $(...).each(function(){
        ... = new PNGPreloader(...);
});

【问题讨论】:

    标签: javascript php wordpress enqueue


    【解决方案1】:

    应该将jquery-ui-widget 添加到wp_enqueue_script 的依赖项参数中。

    function stripesinteriors_theme_resources() { 
        wp_enqueue_script( 'plugins', get_template_directory_uri() . '/javascript/plugins.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-widget' ), '1.0.0', true );
        wp_enqueue_script( 'ui'     , get_template_directory_uri() . '/javascript/ui.js'     , array()                                                , '1.0.0', true );
    }
    

    【讨论】:

      【解决方案2】:

      你能把你的function PNGPreloader(e,t,i,r,n,a){...} 移到jQuery(document).ready(function($) { ... }); 块之外吗?

      这是一个范围界定问题,因为 ui.js 中的代码对 plugins.js 中的 document-ready 调用的函数内部的任何内容都没有可见性。

      或者你也可以这样做:

      plugins.js:

      var fnPNGPreloader;  // globally accessible
      
      jQuery(document).ready(function($) {
          fnPNGPreloader = function (e,t,i,r,n,a) {
              /*.. assuming you do stuff with "$" in here ..*/
          };
          //...
      });
      

      ui.js:

      jQuery(document).ready(function($) {
          $(...).each(function(){
              mobileIcons[ID] = new fnPNGPreloader($object,frames,size[0],size[1],20,false);
          });
      });
      

      【讨论】:

      • 我把它移到了plugins.js,我在浏览器检查器的控制台中得到了一个$ is not a function。我把函数放在ui.js 中并把它移出,我又得到了$ is not a function。我将它们全部放在jQuery(document).ready(function($) {}); 中,因为wordpress 不能与$ 一起使用。
      • 您是否通过函数 PNGPreloader 定义中的$ 运算符引用 jQuery?如果是这样 - 请参阅我上面的编辑,其中函数定义是在准备好的文档中创建的(通过 $ 对 jQuery 的引用),但函数的句柄是全局可访问的。
      • 我得到了一个fnPNGPreloader is not defined,但我确实在函数内部使用了$。这是它的代码:pastebin.com/hzt5XbM8 我认为它与 functions.php 文件中的依赖项参数有关,但我在数组中包含了所有必要的内容。
      • 您从哪里获得is not defined?您将全局变量命名为 fnPNGPreloader,就像我上面在 plugins.js 中的示例一样?
      • 谢谢,我已经解决了:必须包含一个依赖项。 2天后……叹息
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 2019-09-10
      • 2021-10-04
      • 1970-01-01
      相关资源
      最近更新 更多