首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery主题所需的WordPress代码优化

jQuery主题所需的WordPress代码优化
EN

Stack Overflow用户
提问于 2018-02-19 20:52:19
回答 1查看 44关注 0票数 0

我试图使用jQuery实现一个网格,就像WordPress主题中基于scaling.html的谷歌图像一样

代码语言:javascript
复制
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function()
{
    // Detect browser resize and re-calculate
    $(window).bind("resize", calculateContent);

    // Calculate the content on load
    calculateContent();
});

/**
* Function that calculates on how the content should behave.
**/
function calculateContent( e ) {
    // Show all items in case they were hidden
    $("#content img").show();

    // Since we know the width each image takes (100px + 40px + 40px + 2px + 2px = 184px)
    // We can calculate how many rows are showed
    var imageWidth = 184;
    var contentAreaWidth = $("#content").width();
    var itemsPerRow = Math.floor(contentAreaWidth / imageWidth);

    // Retrieve the total number of images
    var totalNrOfItems = $("#content img").length;

    // Calculate how many full rows can be showed
    var fullRows = Math.floor(totalNrOfItems / itemsPerRow);

    // Calculate how many images fell off
    var itemsToHide = totalNrOfItems - fullRows * itemsPerRow;

    // Hide the images by counting backward (from the last item)
    for(var i = totalNrOfItems; i > totalNrOfItems - itemsToHide; i--) {
        // The "eq()" is zero-based, we'll need to substract one
        var remover = i - 1;
        $("#content img:eq("+remover+")").hide();
    }

    // Set debug information
    debug(itemsPerRow, itemsToHide);
}


/**
* Show debug information
**/
function debug(nritems, hidden) {
    $("#debug").html(
        "<p>Items per row: <strong>"+ nritems +"</strong>. Hidden images: <strong>"+ hidden +"</strong>.</p>"
    );
}

这是一个简单的函数,但我无法运行,因为它给了我许多错误,例如: Uncaught : google没有在script.js?ver=20170412:5上定义

如何优化这段jQuery代码以避免错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-19 21:10:17

默默无闻的ReferenceError:谷歌不是在script.js?ver=20170412:5上定义的

这听起来好像您忘了在页面中包含/排队Google Loader脚本。如果是这样,只需将其添加到主题的function.php文件中-例如:

代码语言:javascript
复制
function wp2407_add_scripts() {
    // Rest of your scripts ...
    wp_enqueue_script( 'google-jsapi', 'https://www.google.com/jsapi' );     
}
add_action( 'wp_enqueue_scripts', 'wp2407_add_scripts' );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48873982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档