无法在自定义WordPress主题中运行JS文件

Unable to run JS file in custom WordPress theme

本文关键字:运行 JS 文件 自定义 WordPress      更新时间:2023-09-26

我正在将 Bootstrap 模板转换为 WordPress 主题,我在 functions.php 中引用了我所有的样式表和脚本,并在页脚.php中引用了一些初始化代码,但有一个特定的文件没有正确执行:http://blackrockdigital.github.io/startbootstrap-creative/js/creative.js。

脚本:

(function($) {
    "use strict"; // Start of use strict
    // jQuery for page scrolling feature - requires jQuery Easing plugin
    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: ($($anchor.attr('href')).offset().top - 50)
        }, 1250, 'easeInOutExpo');
        event.preventDefault();
    });
    // Highlight the top nav as scrolling occurs
    $('body').scrollspy({
        target: '.navbar-fixed-top',
        offset: 51
    })
    // Closes the Responsive Menu on Menu Item Click
    $('.navbar-collapse ul li a:not(.dropdown-toggle)').click(function() {
        $('.navbar-toggle:visible').click();
    });
    // Fit Text Plugin for Main Header
    $("h1").fitText(
        1.2, {
            minFontSize: '35px',
            maxFontSize: '65px'
        }
    );
    // Offset for Main Navigation
    $('#mainNav').affix({
        offset: {
            top: 100
        }
    })
    // Initialize WOW.js Scrolling Animations
    new WOW().init();
})(jQuery); // End of use strict

我已经将这个特定的文件设置为使用 jQuery 并通过 functions.php 在 </body> 标签之前调用,但里面的脚本都没有执行,Chrome 返回了此错误:

Uncaught TypeError: $(...).scrollspy is not a function

我尝试用 jQuery 替换$别名,但没有效果。我还确认这是在jQuery库之后调用的。

任何帮助将不胜感激。谢谢!

我想你忘记链接bootstrap.min.js或者正确链接它

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>