Javascript jQuery 未在 Wordpress 中定义,但我的脚本已正确排队

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28248113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:27:53  来源:igfitidea点击:

jQuery is not defined in Wordpress, but my script is enqueued properly

javascriptphpjquerywordpressslidetoggle

提问by nicatoby

I am trying to load a separate javascript file mobile-menu.js to my Wordpress theme. When I look at the console, it says, "jQuery is not defined." However, I know that I enqueued my script files correctly. Any ideas?

我正在尝试将一个单独的 javascript 文件 mobile-menu.js 加载到我的 Wordpress 主题中。当我查看控制台时,它说“未定义 jQuery”。但是,我知道我正确地将脚本文件排入队列。有任何想法吗?

HTML file:

HTML文件:

<a href="#" id="menu-icon"></a> <!--this line wasn't here originally-->
    <div id="switchmenu"><!--switchmenu begin-->
        <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
    </div><!--switchmenu end-->

functions.php file:

函数.php文件:

function lapetitefrog_scripts() {
    wp_enqueue_style( 'lapetitefrog-style', get_stylesheet_uri() );
    wp_enqueue_script( 'lapetitefrog-mobile-menu', get_template_directory_uri() . '/js/mobile-menu.js', array(), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'lapetitefrog_scripts' );

mobile-menu.js file:

mobile-menu.js 文件:

 jQuery(document).ready(function($) {
    $('#menu-icon').click(function() {
            $('#switchmenu').slideToggle("fast");
    });
});

回答by Gavin Simpson

Add wp_enqueue_script('jquery');before you enqueue your scripts. 

wp_enqueue_script('jquery');在将脚本排入队列之前添加。 

回答by jogesh_pi

First Make sure that jquery file is include in the header, and your file requied jQuery

首先确保jquery文件包含在标题中,并且您的文件需要jQuery

wp_enqueue_script( 
        'lapetitefrog-mobile-menu', 
        get_template_directory_uri() . '/js/mobile-menu.js', 
        array('jquery'), 
        '1.0', 
        true 
);

Second you should start your javascript file like:

其次,你应该像这样启动你的 javascript 文件:

(function($) {
    $(document).ready(function() {
        .......
    });
})(jQuery);

OR

或者

// Use jQuery in place of $
jQuery(document).ready(function() {
    .....
});

回答by Faisal Naseer

You can try:

你可以试试:

enqueue Jquery first.

首先排队Jquery。

wp_enqueue_script('jquery'); 

and then enqueuing the latter script with JQuery dependencyin 3rd argument i.e array('jquery')that's what mostly people forget.

然后在第三个参数中使用JQuery 依赖项将后一个脚本排入队列,即array('jquery')大多数人都忘记了这一点。

wp_enqueue_script( 'lapetitefrog-mobile-menu', get_template_directory_uri() . '/js/mobile-menu.js', array('jquery'), '1.0', true );

回答by Floris

The error jQuery is not definedmight also appear with jQuery code embedded in your template parts.

该错误jQuery is not defined也可能与嵌入在模板部件中的 jQuery 代码一起出现。

This happens when jQuery is added to the footerof the page, while the embedded script tries to execute in the middleof the page.

当 jQuery 被添加到页面的页脚,而嵌入的脚本试图在页面中间执行时,就会发生这种情况。

If this is the case, you need to load jQuery in the header with wp_enqueue_script(). So the last parameter needs to be falsehere:

如果是这种情况,您需要在标头中加载 jQuery wp_enqueue_script()。所以最后一个参数在这里需要为

wp_enqueue_script( 'my-jquery', get_template_directory_uri() . '/js/jquery.js', array(), time(), false );

Now the code executes in the right order and jQuery will be defined.

现在代码以正确的顺序执行,jQuery 将被定义。

Another solution would be to move the embedded script to a Javascript file that will also be added with wp_enqueue_script()to the footer of the page. This is up to you.

另一种解决方案是将嵌入的脚本移动到一个 Javascript 文件中,该文件也将添加wp_enqueue_script()到页面的页脚中。这取决于你。

回答by Ruso

Wordpress ships with jQuery by default

Wordpress 默认附带 jQuery

// Adds jQuery
    add_action('init', 'childoftwentyeleven_down');

        function childoftwentyeleven_down() {

          // register your script location, dependencies and version
                wp_register_script('down',
                    get_stylesheet_directory_uri() . '/js/down.js',
                    array('jquery') );
           // enqueue the script
           wp_enqueue_script('down');

        }