需要 JavaScript 帮助(YQL 和 JavaScript)

发布于 2024-10-05 20:31:19 字数 1107 浏览 4 评论 0原文

我有这个“改编”的JavaScript代码来显示任何事情的有趣事实(在本例中是关于电影和/或历史) 然而,它一次只显示一个事实,用户需要按一个按钮才能显示另一个事实。

我的问题是,有没有一种方法可以一次显示所有事实,而不是一次只显示一个事实?

function funfacts(o){
  var facts = document.getElementById('funfacts');
  facts.className = 'js';
  if(facts){
    var data = o.query.results.p;
    var link = facts.getElementsByTagName('a')[0];
    link.innerHTML = '(see all facts)';
    var out = document.createElement('p');
    out.className = 'fact';
    facts.insertBefore(out,link.parentNode);
    function seed(){
      var ran = parseInt(Math.random()*data.length);
      out.innerHTML = data[ran];
    }
    var b = document.createElement('button');
    b.innerHTML = 'get another fact';
    b.onclick = seed;
    link.parentNode.insertBefore(b,link);
    seed();
  }
}

  <script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.tealdragon.net%2Fhumor%2Ffacts%2Ffacts.htm'%20and%20xpath%3D'%2F%2Fli%2Fp'&format=json&diagnostics=true&callback=funfacts"></script>  

I have this "adapted" javascript code to display funfacts about anything (in this case about movies and/or History)
However, it is built to display only one fact at a time, the user needs to press a button to display another funfact.

My question is, is there a way to display all the facts at once and not just one at a time?

function funfacts(o){
  var facts = document.getElementById('funfacts');
  facts.className = 'js';
  if(facts){
    var data = o.query.results.p;
    var link = facts.getElementsByTagName('a')[0];
    link.innerHTML = '(see all facts)';
    var out = document.createElement('p');
    out.className = 'fact';
    facts.insertBefore(out,link.parentNode);
    function seed(){
      var ran = parseInt(Math.random()*data.length);
      out.innerHTML = data[ran];
    }
    var b = document.createElement('button');
    b.innerHTML = 'get another fact';
    b.onclick = seed;
    link.parentNode.insertBefore(b,link);
    seed();
  }
}

  <script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.tealdragon.net%2Fhumor%2Ffacts%2Ffacts.htm'%20and%20xpath%3D'%2F%2Fli%2Fp'&format=json&diagnostics=true&callback=funfacts"></script>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2024-10-12 20:31:19

变量data包含数组中的所有事实。当用户单击时,函数 seed 会从数组中随机选择一项。

因此,要显示全部内容:

out.innerHTML = data.join("<br />");

这会将事实连接到单个字符串中,并用换行符分隔。

编辑添加:

这是一个完整、非常简单的网页示例,显示所有项目:

<html>
<body>
<div id="funfacts">
</div>
<script>
function funfacts(o){
  var facts = document.getElementById('funfacts');
  if(facts){
    var data = o.query.results.p;
    var out = document.createElement('p');
    out.className = 'fact';
    facts.appendChild(out);
    out.innerHTML = data.join("<br />");
  }
}
</script>
<script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.tealdragon.net%2Fhumor%2Ffacts%2Ffacts.htm'%20and%20xpath%3D'%2F%2Fli%2Fp'&format=json&diagnostics=true&callback=funfacts"></script>

</body>
</html>

The variable data contains all of the facts in an array. When the user clicks, the function seed is randomly selecting one item out of the array.

So, to display them all:

out.innerHTML = data.join("<br />");

This joins the facts into a single string, separated by newlines.

Edited to add:

Here's an example of a complete, very simple web page that displays all of the items:

<html>
<body>
<div id="funfacts">
</div>
<script>
function funfacts(o){
  var facts = document.getElementById('funfacts');
  if(facts){
    var data = o.query.results.p;
    var out = document.createElement('p');
    out.className = 'fact';
    facts.appendChild(out);
    out.innerHTML = data.join("<br />");
  }
}
</script>
<script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.tealdragon.net%2Fhumor%2Ffacts%2Ffacts.htm'%20and%20xpath%3D'%2F%2Fli%2Fp'&format=json&diagnostics=true&callback=funfacts"></script>

</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文