有没有办法在 WordPress 中关闭 jQuery noConflict 模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17687619/
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-26 19:44:34  来源:igfitidea点击:

Is there a way turn off jQuery noConflict mode in WordPress?

jquerywordpress

提问by AidanCurran

Is there a way turn off jQuery.noConflictin WordPress? I don't mean loading an alternative version of jQuery or changing the loading method ie:

有没有办法jQuery.noConflict在 WordPress 中关闭?我的意思不是加载 jQuery 的替代版本或更改加载方法,即:

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

or

或者

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

I mean is there a way to just turn off noConflictmode for the version of jQuery bundled with WordPress?

我的意思是有没有办法关闭noConflict与 WordPress 捆绑的 jQuery 版本的模式?

Like would setting jQuery.noConflict(false)work? If so, where would you set it?

喜欢设置jQuery.noConflict(false)工作吗?如果是这样,你会在哪里设置它?

采纳答案by Seth C.

After some research, this is the best answer I can give you:

经过一番研究,这是我能给你的最佳答案:

$ = jQuery.noConflict(true);

To answer your other question, no you can't pass in false, the attribute is used to control what happens to global variables. You can find the documentation here: http://api.jquery.com/jQuery.noConflict/

要回答您的另一个问题,不,您不能传入 false,该属性用于控制全局变量发生的情况。您可以在此处找到文档:http: //api.jquery.com/jQuery.noConflict/

Also note you could load 2 different versions of jQuery as it suggests (but is not recommended).

另请注意,您可以按照建议加载 2 个不同版本的 jQuery(但不推荐)。

回答by DMTintner

If you are including your own javascript library or scripts you can add the following to the very top:

如果您包含自己的 javascript 库或脚本,您可以将以下内容添加到最顶部:

 var $ = jQuery;

回答by JHoffmann

I found another way of making the variable $available globally. Just put the following in your theme's functions.php or in a plugin:

我找到了另一种使变量$全局可用的方法。只需将以下内容放入主题的 functions.php 或插件中:

function so17687619_jquery_add_inline() {
    wp_add_inline_script( 'jquery-core', '$ = jQuery;' );
}
add_action( 'wp_enqueue_scripts', 'so17687619_jquery_add_inline' );

This will output $ = jQuery;as an inline script immediately after the script tag for jQuery. So any scripts included after have the jQuery instance available as $and jQuery.

这将$ = jQuery;在 jQuery 的脚本标记之后立即输出为内联脚本。因此,之后包含的任何脚本都将 jQuery 实例作为$和可用jQuery

回答by Mike

Adding this worked for me finally:

添加这个最终对我有用:

var $ = jQuery.noConflict();

You can add this in your header.php file in the head section:

您可以在 head 部分的 header.php 文件中添加它:

<script>var $ = jQuery.noConflict();</script>

Or if you use child theme, add that to functions.php in your child theme directory:

或者,如果您使用子主题,请将其添加到子主题目录中的 functions.php 中:

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri() . '/main.js',
        array( 'jquery' )
    );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

And create a main.js file in the same location as functions.php (in the child theme direcotry) and in that file add this:

并在与functions.php相同的位置创建一个main.js文件(在子主题目录中)并在该文件中添加以下内容:

var $ = jQuery.noConflict();

回答by vimes1984

To turn if off go to your wp-includes/js/jquery/jquery.jsfile and remove the jQuery.noConflict()from the last line. Or as you suggested just set the boolean to false.

要关闭,请转到您的wp-includes/js/jquery/jquery.js文件并jQuery.noConflict()从最后一行中删除。或者按照您的建议,只需将布尔值设置为 false。

That or you could replace the contents with a clean download from jquery.com, there is an intense debate going on in the trac.

那或者你可以用从 jquery.com 干净的下载来替换内容,在trac 中正在进行激烈的辩论。