加载到页脚时,Javascript文件无法正常工作

时间:2018-01-26 11:09:53

标签: javascript php html css wordpress

好的,我正在开发一个拥有自己的自定义插件的网站。我想让这个插件的所有js文件加载到页脚,因为它减慢了网站的速度。我知道如何让它们加载到页脚,但是当我这样做时,js文件似乎不再起作用,即使我在页面检查时可以在页脚中看到它们。有人知道是什么原因引起的吗?

function enqueue_stairbuilder_js()
{

wp_register_script('simplemodal', plugins_url('js/jquery.simplemodal.1.4.4.min.js', __FILE__), array( 'jquery' ), "1.4.4", true);
wp_register_script('paper', plugins_url('js/paper.js', __FILE__), array( 'jquery' ),"1.0.1", true);
wp_register_script('prices', plugins_url('js/prices.js', __FILE__), array( 'jquery' ), "", true);
wp_register_script('popup', plugins_url('js/popup.js', __FILE__), array( 'jquery' ), "1.0.3", true);
wp_register_script('printelement', plugins_url('js/printElement.js', __FILE__), array( 'jquery' ), "1.0.3", true);
wp_register_script('stairbuilder', plugins_url('js/stairbuilder.js', __FILE__), array( 'jquery' ),"1.0.3", true);        
wp_register_script('drawing', plugins_url('js/drawing.js', __FILE__), array( 'jquery' ),"1.0.3", true);
wp_register_script('deserialise', plugins_url('js/deserialise.js', __FILE__), array( 'jquery' ),"1.0.3", true);

wp_enqueue_script('simplemodal');
wp_enqueue_script('paper');
wp_enqueue_script('prices');
wp_enqueue_script('popup');
wp_enqueue_script('printelement');
wp_enqueue_script('stairbuilder');
wp_enqueue_script('drawing');
wp_enqueue_script('deserialise');

}
add_action( 'wp_enqueue_scripts', 'enqueue_stairbuilder_js' ); 

这是我的函数文件的内容,因为我在这里做错了导致这个。

<?php


    //custom gallery image size, cropped
    add_image_size( 'gallery-thumb', 720, 720, true );

    //removing toolbar on front end
    function remove_toolbar(){
        if ( is_blog_admin() ) {
            return true;
        }
        remove_action( 'wp_head', '_admin_bar_bump_cb' );
        return false;
    }
    add_filter( 'show_admin_bar', 'remove_toolbar' );

    //replacing default jQuery with new version
    function update_jQuery(){
        wp_deregister_script('jquery');
        wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', '3.2.1');
    } 
    add_action('wp_enqueue_scripts', 'update_jQuery');

    //MY SCRIPTS
    function my_script() {
        wp_enqueue_script('custom_js', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'));
    }
    add_action('wp_enqueue_scripts', 'my_script');

    //BOOTSTRAP
    function bootstrap(){
        wp_register_script('bootstrap_js', get_template_directory_uri(). '/bootstrap/js/bootstrap.min.js', array('jquery'), '3.3.6', true);
        wp_enqueue_script('bootstrap_js');
        wp_enqueue_style('bootstrap_css', get_template_directory_uri() . '/bootstrap/css/bootstrap.min.css', '3.3.7',true); 
    }
    add_action( 'wp_enqueue_scripts', 'bootstrap');

    function my_styles(){
        wp_enqueue_style('theme_styles', get_template_directory_uri(). '/css/my_styles.css', true);
    }
    add_action('wp_enqueue_scripts', 'my_styles');

    //magnific img popup
    function magnific_popup(){
        wp_register_script('magnific_js', get_template_directory_uri(). '/js/magnific_popup.js', array('jquery'), '', true);
        wp_enqueue_style('magnific_css', get_template_directory_uri(). '/css/magnific_popup.css');
        wp_enqueue_script('magnific_js');
    }
    add_action('wp_enqueue_scripts', 'magnific_popup');

    /*NAV MENUS*/
    function register_my_menus() {
        register_nav_menus(array(
            'head_nav' => __( 'Primary' ),
            'foot_nav' => __( 'Secondary' ),
            'side_bar_menu' => __( 'side_bar' )
        ) );
    }
    add_action( 'init', 'register_my_menus' );

    //enqueuing default style sheet
    wp_enqueue_style('default_style', get_stylesheet_uri());

    require get_template_directory() . '/woo_hooks.php';
?>

2 个答案:

答案 0 :(得分:0)

尝试这个例子:

function wpdocs_theme_name_scripts() {
   wp_enqueue_script( 'xxx', 'https://cdnjs.cloudflare.com/ajax/libs/xxx/2.2.0/xxx.js', array('jquery'), 3.3, true); 
   wp_enqueue_script( 'xxy', 'https://cdnjs.cloudflare.com/ajax/libs/xxy/2.2.0/xxy.js', array('jquery'), 3.3, true); 

}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );

答案 1 :(得分:0)

尝试清除缓存。 The codex also recommends如果您在嵌套在插件目录子目录中的文件中使用plugins_url()函数,则应使用PHP的dirname()函数:

所以试试这个。

function enqueue_stairbuilder_js()
{
    wp_register_script('simplemodal', plugins_url('js/jquery.simplemodal.1.4.4.min.js', dirname(__FILE__)), array( 'jquery' ), "1.4.4", true);
    wp_register_script('paper', plugins_url('js/paper.js', dirname(__FILE__)), array( 'jquery' ),"1.0.1", true);
    wp_register_script('prices', plugins_url('js/prices.js', dirname(__FILE__)), array( 'jquery' ), "", true);
    wp_register_script('popup', plugins_url('js/popup.js', dirname(__FILE__)), array( 'jquery' ), "1.0.3", true);
    wp_register_script('printelement', plugins_url('js/printElement.js', dirname(__FILE__)), array( 'jquery' ), "1.0.3", true);
    wp_register_script('stairbuilder', plugins_url('js/stairbuilder.js', dirname(__FILE__)), array( 'jquery' ),"1.0.3", true);        
    wp_register_script('drawing', plugins_url('js/drawing.js', dirname(__FILE__)), array( 'jquery' ),"1.0.3", true);
    wp_register_script('deserialise', plugins_url('js/deserialise.js', dirname(__FILE__)), array( 'jquery' ),"1.0.3", true);

    wp_enqueue_script('simplemodal');
    wp_enqueue_script('paper');
    wp_enqueue_script('prices');
    wp_enqueue_script('popup');
    wp_enqueue_script('printelement');
    wp_enqueue_script('stairbuilder');
    wp_enqueue_script('drawing');
    wp_enqueue_script('deserialise');

}
add_action( 'wp_enqueue_scripts', 'enqueue_stairbuilder_js' );