随机的 Blogger 按钮 'post, search and combine labels'

Blogger button for random 'post, search and combine labels'

我正在使用下面的 javascript 和 html 创建一个按钮,该按钮 returns 从(单个)标签随机发布。我的博客是:https://funatonce.blogspot.com

我有两个问题:

  1. 如何将此脚本与搜索词列表一起使用。我将输入一组搜索词,每次点击都会给出不同的结果

  2. 我想为不同的标签添加多个具有随机功能的按钮,例如。如果我有 15 个按钮,是否必须在页面上添加此脚本 15 次?我尝试提供 .js 文件参考,但它没有用

    http://你的javascript.com/8012111731/random.js';类型='text/javascript'/>

<script type='text/javascript'>
//<![CDATA[
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/-/cool%20items?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
   }
function readLucky(root){
    var feed = root.feed;
    var total = parseInt(feed.openSearch$totalResults.$t,10);
    var luckyNumber = Math.floor(Math.random()*total);
    luckyNumber++;
    fetchLuck(luckyNumber);
    }
function beautyandpersonalcare(){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '/feeds/posts/summary/-/cool%20items?max-results=0&alt=json-in-script&callback=readLucky';
    document.getElementsByTagName('head')[0].appendChild(script);
    }
//]]>
</script>
<div class="randombtncontainer">
  <a href="#" onclick="beautyandpersonalcare()" class="randombtn btn-1">Show Random Products</a>
  
</div>

您可以使用 jQuery ajax 和 blogger 上的本机搜索功能而不是 JSON feed

来获取搜索结果
<div id="container"></div>

<script>
//<![CDATA[
var counter = 0;
function getList(terms) {
  $("#container").load("/search?q="+terms[counter].replace(' ', '+')+" #Blog1 .post");
  counter++;
}
//]]>
</script>

<button onclick="getList(['mobile cases','mobile accessories','mobile apps'])">Show Random List</button>

要添加多个不同标签的按钮,你不需要像在js文件中那样重复你的函数,在beautyandpersonalcare()中将标签作为参数传递并添加你喜欢的按钮。

<script type='text/javascript'>
//<![CDATA[

var label = "";

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/-/'+label+'?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
   }
function readLucky(root){
    var feed = root.feed;
    var total = parseInt(feed.openSearch$totalResults.$t,10);
    var luckyNumber = Math.floor(Math.random()*total);
    luckyNumber++;
    fetchLuck(luckyNumber);
    }
function beautyandpersonalcare(lab){
    label = lab;
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '/feeds/posts/summary/-/'+label+'?max-results=0&alt=json-in-script&callback=readLucky';
    document.getElementsByTagName('head')[0].appendChild(script);
    }
//]]>
</script>

<div class="randombtncontainer">
  <a href="#" onclick="beautyandpersonalcare('labelName1')" class="randombtn btn-1">Show Random Products</a>
  <a href="#" onclick="beautyandpersonalcare('labelName2')" class="randombtn btn-2">Show Random Products</a>
  <a href="#" onclick="beautyandpersonalcare('labelName3')" class="randombtn btn-3">Show Random Products</a>
  ...

</div>