【问题标题】:wordpress jquery version conflictswordpress jquery 版本冲突
【发布时间】:2017-09-27 07:59:00
【问题描述】:

在 Wordpress 中使用钩子 wp_enqueue_script 覆盖 jQuery 版本,但它不起作用。而且我需要两个 jquery。

我想为网站定制添加新的 jquery 版本。

但是当我添加新版本的 jquery 时,旧版本的 jquery 功能不起作用

函数.php:

function themeslug_enqueue_script(){
    wp_enqueue_script('child_theme_script_handle', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js',
        array( 'jquery' ), '3.2.1', true );
    wp_enqueue_script( 'my-great-script', get_stylesheet_directory_uri() . '/core/assets/js/multi-vendors.js', 
        array( 'jquery' ), '1.0', true );
}

add_action('wp_enqueue_scripts', 'themeslug_enqueue_script');

jQuery.js:

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

某些功能无法正常工作。比如滑块。

【问题讨论】:

  • 在 wordpress 中你必须使用 jQuery 而不是 $
  • 不工作,同样的问题
  • 我们可以查看网站以检查是否所有内容都包含在内
  • 我在本地环境中工作

标签: javascript php jquery wordpress


【解决方案1】:

您需要先注销旧版本脚本并将新版本加入队列。 请使用此代码更改 jquery 版本。

<?php  function modify_jquery() {
    if (!is_admin()) { 
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', false, '1.7.2');
        wp_enqueue_script('jquery');
    }
 }

add_action('init', 'modify_jquery'); ?>

【讨论】:

  • 感谢您的回答,但我的问题是我想要两个 jquery。
  • 可以多次使用。像这样 wp_deregister_script('my-great-script'); wp_register_script('my-great-script', 'github.com/flesler/jquery.scrollTo/blob/master/…', false, '2.1.2' ); wp_enqueue_script('my-great-script');
  • 当您取消注册任何 jquery 并使用新 jquery 排队时,新 jquery 就可以使用而不是旧 jquery。
【解决方案2】:

感谢大家提出建议。

我将我的代码放在 custom.js 文件中,它的工作。 不需要添加新的jquery。

很抱歉误导了你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多