首页 文章

在Google自定义搜索(CSE)上添加'Next'和'Previous'链接

提问于
浏览
1

是否可以在Google自定义搜索中添加指向页面结果的下一个和上一个链接?我找到了这个帖子How to show next/previous links in Google Custom Search Engine paging links但代码是旧版Google自定义搜索 .

较新的版本只为您提供了一个像 <gcse:searchresults-only></gcse:searchresults-only> 的标签,一切都是从那里生成的 . 有任何想法吗?谢谢

这就是搜索结果的结果 .

<script>
(function() {
    var cx = 'xxxxxx:xxxxx',
        gcse = document.createElement('script');

    gcse.type = 'text/javascript'; gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>

<gcse:searchresults-only></gcse:searchresults-only>

1 回答

  • 0

    您可以使用包含在 google.setOnLoadCallback 中的所有 searcher.cursor.currentPageIndexsearcher.gotoPage()

    window.customSearchControl = customSearchControl;
    
    $(function() {
    
        var link = $('<a />', {
            'html' : 'link',
            'href' : '#',
            'class' : 'general-button',
            'style' : 'margin-right:1em'
        });
    
        var searcher = window.customSearchControl.getWebSearcher();
    
        var prev = link.clone().click(function(e) {
            e.preventDefault();
            var topage = searcher.cursor.currentPageIndex - 1;
            if(topage < 1) {
                topage = 0;
            }
            searcher.gotoPage(topage);
            return false;
        }).html('&lt; prev');
    
        var next = link.clone().click(function(e) {
            e.preventDefault();
            var topage = searcher.cursor.currentPageIndex + 1;
            if(topage > searcher.cursor.pages.length) {
                topage = 10;
            }
            searcher.gotoPage(topage);
            return false;
        }).html('next &gt;');
    
        $('#cse').append(prev).append(next);
    });
    

相关问题