为什么wordpress中排队的脚本无法正常工作? [英] Why isn't enqueued script in wordpress working?

查看:98
本文介绍了为什么wordpress中排队的脚本无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用functions.php的Wordpress子主题,其中包含以下内容:

I have a Wordpress childtheme using a functions.php that contains the following:

    <?php
        add_action( 'wp_enqueue_scripts', 'load_custom_script' ); 
        function load_custom_script() { 
        wp_register_script( 'custom-script', get_template_directory_uri() . '/js/wpw-custom.js',   array('jquery'), '1.0', true ); 
        wp_enqueue_script( 'custom-script' );
      }
   ?>

这已成功加载我的自定义脚本,在页脚中,jQuery本身已加载完毕.自定义脚本包含以下测试代码,以让我知道入队过程是否有效:

This is successfully loading my custom script, in the footer, AFTER jQuery itself loads. The custom script contains the following testing code to let me know if the enqueue process works:

$( document ).ready(function() {
alert( "ready!" );
});

我可以在页面上查看源代码并验证是否成功加载了wpw-custom.js,但它没有执行警报!

I can view source on my pages and verify the successful loading of my wpw-custom.js but it is not executing the alert!

我做错了什么?我可以采取哪些步骤来有条不紊地找出我做错了什么?非常感谢您的帮助!

What have I done wrong or what steps can I take to methodically find out what I have done wrong? Many thanks for any help!

推荐答案

WordPress中的jQuery在noConflict模式下运行,这意味着jQuery的全局$快捷方式不可用.用以下代码替换您的代码:

jQuery in WordPress runs in noConflict mode which means the global $ shortcut for jQuery isn't available. Replace your code with the following:

jQuery(document).ready(function($) {
    alert( 'ready!' );
});

在文档准备好的包装器中,$可以用作jQuery的别名.

Inside the document ready wrapper the $ can be used as an alias for jQuery.

这篇关于为什么wordpress中排队的脚本无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆