1

我的博客网站上有一个 JavaScript 按钮,可将您带到随机帖子。它的代码如下。有没有办法更改此代码以将用户带到下一个或上一个帖子?

<span class="ranlink" id="myLuckyPost"></span> 
<script type="text/javascript">
function showLucky(root) {
    var feed = root.feed;
    var entries = feed.entry || [];
    var entry = feed.entry[0];
    for (var j = 0; j < entry.link.length; ++j) {
        if (entry.link[j].rel == 'alternate') {
            window.location = entry.link[j].href;
        }
    }
}
function fetchLuck(luck) {
    script = document.createElement('script');
    script.src = '/feeds/posts/summary?start-index=' + luck + '&max-results=1&alt=json-in-script&callback=showLucky';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
}
function feelingLucky(root) {
    var feed = root.feed;
    var total = parseInt(feed.openSearch$totalResults.$t, 10);
    var luckyNumber = Math.floor(Math.random() * total);
    luckyNumber++;
    a = document.createElement('a');
    a.id = 'navlink';
    a.href = '#random';
    a.rel = luckyNumber;
    a.onclick = function () {
        fetchLuck(this.rel);
    };
    a.innerHTML = 'Random Song';
    document.getElementById('myLuckyPost').appendChild(a);
}
</script>
<script src="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky"></script>
4

1 回答 1

2

我不确定您的 URL 模式。但根据你的脚本,它必须与luckyNumber有关:

var luckyNumber = Math.floor(Math.random() * total);

因此,将此随机数更改为下一篇/上一篇文章的相应编号将解决您的问题。

于 2012-05-03T10:17:42.127 回答