我的JavaScript文件未在WordPress中加载

时间:2017-07-06 23:28:25

标签: javascript php jquery wordpress

我正在尝试在WordPress中运行一个灯箱插件。由于某种原因,它将无法加载,我有正确的文件路径。通常我使用cdn版本的文件,但是没有这样的版本,我必须在本地加载它。我也有一些违规行为。我不知道发生了什么以及为什么我收到错误。

的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错误

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

违反

[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 个答案:

答案 0 :(得分:0)

您正在Lightbox脚本之前加载custom.js.

尝试为您的队列添加额外的依赖项

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

或者添加lightBox的脚本是什么。您应该始终确保正确设置依赖关系以避免这种情况。

注意:您还应该确保如果有一个需要jQuery的入队脚本(如lightBox),请将其设置为依赖项。