douwei3280 2017-07-06 23:28 采纳率: 0%
浏览 126
已采纳

我的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 2017-07-07 00:58
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料