使用JavaScript查询Fusion Tables并选择3个随机项

时间:2016-06-13 18:46:49

标签: javascript random google-fusion-tables

我使用Fusion Tables将网址存储到我的Google云端硬盘照片(存储在链接列中)。

我使用JavaScript查询Fusion Tables以获取所有网址,然后创建一个包含从数据中随机挑选的3个项目的表格。

我在下面添加的代码让我看到3张第一张照片......我试图切换线

contentStr += "<td><img src=" + item[j]+"></td>";

contentStr += "<td><img src=" + randomElement[j]+"></td>";

但它不起作用。有人能帮帮我吗?

<head>
    <title>Test</title>
  </head>
  <body>
    <div id="content"></div>
    <script>
    function handler(response) {
      var maxFoto = response.rows.length
      var contentStr = "<table>";
      for (var i = 0; i < 3; i++) {
        var item = response.rows[i];
          var randomFoto = Math.floor(Math.random()*maxFoto);
          var randomElement = item[randomFoto];
        contentStr += "<tr>";
        for (var j = 0; j < item.length; j++) {
          contentStr += "<td><img src=" + item[j]+"></td>";
        }
        contentStr += "</tr>";
      }
      contentStr += "</table>";
      document.getElementById("content").innerHTML = contentStr;
    }
    </script>
    <script src="https://www.googleapis.com/fusiontables/v2/query?sql=SELECT%20Link%20FROM%20MYidTABLE&key=MYKEY&callback=handler&viewable=true"></script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

我给自己一个答案......下面的代码完美无缺!

<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <div id="content"></div>
    <script>
    function handler(response) {
      var maxFoto = response.rows.length
      var contentStr = "<table>";
      for (var i = 0; i < 3; i++) { 
        var randomFoto = Math.floor(Math.random()*maxFoto);
        var item = response.rows[randomFoto];
        contentStr += "<tr>";
        for (var j = 0; j < item.length; j++) {
          contentStr += "<td><img src=" + item[j]+"></td>";
        }
        contentStr += "</tr>";
      }
      contentStr += "</table>";
      document.getElementById("content").innerHTML = contentStr;
    }
    </script>
    <script src="https://www.googleapis.com/fusiontables/v2/query?sql=SELECT%20Link%20FROM%20MYTABLEID&key=MYKEY&callback=handler&viewable=true"></script>
  </body>
</html>