jQuery需要单词Jquery而不是$ [英] jquery requires the word Jquery instead of $

查看:128
本文介绍了jQuery需要单词Jquery而不是$的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究WordPress主题,并且包含了Jquery,

I am working on a WordPress theme and I have included Jquery,

    // Waves
    wp_enqueue_style( 'APKMirror-wave', get_template_directory_uri() . '/inc/waves/waves.min.css');
    wp_enqueue_script(
        'APKMirror-wave',
        get_stylesheet_directory_uri() . '/inc/waves/waves.min.js',
        array( 'jquery' )
    );

    // Boostrap
    wp_enqueue_style( 'APKMirror-Bootstrap', get_template_directory_uri() . '/inc/bootstrap/bootstrap.min.css');
    wp_enqueue_script(
        'APKMirror-Bootstrap',
        get_stylesheet_directory_uri() . '/inc/bootstrap/bootstrap.min.js',
        array( 'jquery' )
    );

问题是,无论何时我运行jQuery代码,我都必须键入单词"jQuery",而不仅仅是使用$.如何更改我的代码以使其使用$?

The problem is any time I run jQuery code I have to type the word 'jQuery' instead of just using the $. How can I change my code so that it will use $?

推荐答案

WordPress中的jQuery在noConflict模式下运行,这意味着jQuery的全局$快捷方式不可用.使用文档准备好的包装器,该包装器将允许$用作jQuery的别名.

jQuery in WordPress runs in noConflict mode which means the global $ shortcut for jQuery isn't available. Use the document ready wrapper which will allow $ to be used as an alias for jQuery.

jQuery(document).ready(function($) {
    // Inside here $ can be used instead of jQuery.
});

(function($) {
    // Use $ here.
})(jQuery);

感谢 Paulpro 正确地指出,如果您需要在其余DOM加载之前运行脚本,则可以使用替代方法.

Thanks to Paulpro for correctly pointing out that there is an alternative that can be used if you need to run the script before the rest of the DOM loads.

这篇关于jQuery需要单词Jquery而不是$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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