2

我需要一些帮助,我目前正在将我网站上的所有内部脚本移植到一个js文件中,以根据 Google 和 Yahoo 页面速度建议加快我的网站速度。

我的页面 footer.php 中有以下脚本(我正在使用 wordpress)

<script> (function ($) {
jQuery(function () {
    var $container = $('#container');
    $container.imagesLoaded(function () {
        $container.masonry({
            itemSelector: '.box',
            columnWidth: 100
        });
    });
    $container.infinitescroll({
        navSelector: '.post-nav',
        nextSelector: '.previous a',
        itemSelector: '.box',
        loading: {
            finishedMsg: 'No more pages to load.',
            img: '/wp-content/themes/images/ajax-loader.gif'
        }
    },

    function (newElements) {
        var $newElems = $(newElements).css({
            opacity: 0
        });
        $newElems.imagesLoaded(function () {
            $newElems.animate({
                opacity: 1
            });
            $container.masonry('appended',
            $newElems, true);
        });
    });
});
})(jQuery) 
</script>

如果脚本在我的页脚中,它可以 100% 工作,当我将它添加到单个 js 中时,它会停止工作......

关于为什么会这样的任何想法,以及我应该如何从外部 js 样式表加载脚本,因为它像内联时一样执行。

4

1 回答 1

1

在外部 js 文件中使用时删除开始<script>和结束标记。</script>还要确保你jQuery在这个 js 文件之前包含。

于 2013-07-21T14:46:04.157 回答