【问题标题】:How to include jquery, jquery-ui in wordpress如何在 wordpress 中包含 jquery、jquery-ui
【发布时间】:2017-02-10 00:09:46
【问题描述】:

如何在 wordpess 中包含 jquery、jquery-ui-core 和 https://www.paytabs.com/theme/express_checkout/js/jquery-1.11.1.min.js。我收到一个错误“Uncaught TypeError: jQuery(...).UItoTop is not a function”。这是一段代码:

function magikCreta_load_jquery(){
      wp_enqueue_script('jquery');
      wp_enqueue_script('jquery-ui-core');
      wp_enqueue_script('jquery-ui', 'https://www.paytabs.com/theme/express_checkout/js/jquery-1.11.1.min.js', array('jquery-ui-core'));
    }

<?php echo '<script>
            jQuery(document).ready(function($){
                Paytabs("#express_checkout").expresscheckout({
});</script>';

【问题讨论】:

  • 你意识到jquery-1.11.1.min.js文件只是jQuery,而不是jQuery-UI?加载第二个 jQuery 核弹任何以前加载的插件
  • 这里也没有任何内容表明您正在加载包含缺失函数的插件脚本

标签: jquery wordpress


【解决方案1】:

请通过下面的代码,希望有所帮助

add_action('wp_enqueue_scripts', 'my_enqueue_scripts');
function my_enqueue_scripts() 
 {
      //the array with jquery and jquery-ui-core are dependency for your file i.e my-custom.js 
    wp_enqueue_script('my-custom-js', get_stylesheet_directory_uri().'/my-custom.js', array('jquery', 'jquery-ui-core'));  
}

并将您的自定义代码放入 my-custom.js

jQuery(document).ready(function($){
                Paytabs("#express_checkout").expresscheckout({
});

【讨论】:

  • 是的,这会在页脚中加载 my-custom.js 并且没有初始化我的 div
【解决方案2】:

最好的方法是加载一个单独的文件。

add_action('wp_enqueue_scripts', 'so13452_enqueue_scripts');
function so13452_enqueue_scripts() {
    // The array('jquery', 'jquery-ui-core') will force jquery and jquery-ui-core from core to be included
    wp_enqueue_script('name', get_stylesheet_directory_uri().'/file.js', array('jquery', 'jquery-ui-core'));

    // Only include jquery core
    wp_enqueue_script('jquery');

    // Jquery ui core
    wp_enqueue_script('jquery-ui-core');

}

你也可以内联添加:

add_action('wp_head', 'so13453_enqueue_scripts');
function so13453_enqueue_scripts() {
    ?>
        <script>
            //
        </script>
     <?php
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多