douwei3280
douwei3280
采纳率0%
2017-07-06 23:28

我的JavaScript文件未在WordPress中加载

I am trying to run a lightbox plugin in WordPress. For some reason it will not load and I have the correct file path. Normally I use the cdn version of files, but there is not a such version around and I have to load it locally. I also have some violations. I have no idea what is going on and why I am receiving the error.

functions.php

function nutrition_club_theme_script_enqueue() {


 wp_enqueue_script('jquery', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js');



    wp_enqueue_script('jquery-tools', get_template_directory_uri() .'/js/jquery.tools.min.js', array('jquery'), '', true);

    wp_enqueue_script('jquery-portfoliolightbox', get_template_directory_uri() .'/js/jquery.portfoliolightbox.js', '', '', true);

    wp_enqueue_script('jquery-magnific-popup', 'https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js');


    wp_enqueue_script('customjs', get_template_directory_uri() . '/js/nutrition_club_theme.js', array('jquery'), '4.8', false);



    wp_enqueue_script('owljs', 'https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js');


    wp_enqueue_script('bootstrapjs', get_template_directory_uri() . '/js/bootstrap.min.js', array(), '3.3.7', true);

    wp_enqueue_script('popup-galleryjs', get_template_directory_uri() . '/js/popup-gallery.js', array(), '', true);

    wp_enqueue_script('wowjs', 'https://cdnjs.cloudflare.com/ajax/libs/wow/1.1.2/wow.min.js');




}


add_action(wp_enqueue_scripts, 'nutrition_club_theme_script_enqueue');

custom.js

     jQuery(".service-popup").lightBox({
    imgpath: 'https://unsplash.it/1200/900?image=839',
    title: 'Your Title',
    text: 'Enter any text',
    link: ['http://www.link1.com', 'http://www.link2.com'],
    linkText: ['Site 1 Text', 'Link 2 text']
});     

Chrome error

Uncaught TypeError: jquery-3.2.1.min.js?ver=3.2.1:2 
jQuery(...).lightBox is not a function
        at HTMLDocument.<anonymous> (nutrition_club_theme.js?ver=4.8:81)
        at j (jquery-3.2.1.min.js?ver=3.2.1:2)
        at k (jquery-3.2.1.min.js?ver=3.2.1:2)
    (anonymous) @ nutrition_club_theme.js?ver=4.8:81
    j @ jquery-3.2.1.min.js?ver=3.2.1:2
    k @ jquery-3.2.1.min.js?ver=3.2.1:2

Violations

[Violation] Added non-passive event listener to a scroll-blocking 'touchstart' event. Consider marking event handler as 'passive' to make the page more responsive.
jquery-3.2.1.min.js?ver=3.2.1:2 [Violation] 'setTimeout' handler took 341ms
[Violation] Forced reflow while executing JavaScript took 197ms
[Violation] Forced reflow while executing JavaScript took 65ms
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dousong5161 dousong5161 4年前

    You are loading custom.js before the Lightbox script.

    Try adding extra dependencies to your enqueue

    wp_enqueue_script('customjs', get_template_directory_uri() . '/js/nutrition_club_theme.js', array('jquery', 'jquery-portfoliolightbox', 'jquery-magnific-popup'), '4.8', false);
    

    Or whatever the script adding the lightBox is. You should always make sure you set dependencies correctly to avoid this.

    NOTE: you should also make sure that if there is an enqueued script ( like the lightBox one ) that requires jQuery, has it set as a dependency.

    点赞 评论 复制链接分享