添加jQuery依赖项会阻止我的脚本入队/加载

时间:2017-02-23 作者:armadadrive

这项工作:

add_action(\'wp_enqueue_scripts\', \'custom_script_enqueue\');
function custom_script_enqueue() {
  wp_enqueue_style(
       \'customstyle\',
        get_template_directory_uri() . \'/css/theme.css\',
        array(),
        \'0.1\',
        \'all\'
  );

  wp_enqueue_script(
     \'customjs\',
      get_template_directory_uri() . \'/js/js.js\',
      array(),
      \'0.1\',
      true
  );
}
但是我的自定义JS将使用几个jQuery调用。当前它失败了,因为jQuery是在这个自定义脚本之后加载的,所以jQuery返回为undefined 页面加载时。

我在这里读了一些答案,并尝试了以下方法:

add_action(\'wp_enqueue_scripts\', \'custom_script_enqueue\');
function custom_script_enqueue() {
  wp_enqueue_style(
      \'customstyle\',
       get_template_directory_uri() . \'/css/theme.css\',
       array(),
      \'0.1\',
      \'all\'
   );

   wp_enqueue_script(
      \'customjs\',
      get_template_directory_uri() . \'/js/js.js\',
      array(\'jQuery\'),
      \'0.1\',
      true
   );
}
现在,我的自定义脚本根本不加载。当我view-source: 在页面上。

我试着替换wp_enqueue_script 具有wp_register_script 然后打电话wp_enqueue_script(\'customjs\'); 下面,虽然它没有抛出错误,但我得到了相同的(缺少)结果。

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

jQuery依赖项的句柄实际上是,jquery (所有小写和区分大小写)。

记录了JavaScript句柄的完整列表here 在主题开发人员手册中。