如何制作这个javascript,用它的标签显示随机帖子?

时间:2021-11-24 07:22:29

I have searching about random post and it's show Math.random() function, but because my lack of knowledge about javascripting so I need your help to make this javascript work on random, thanks.

我搜索了随机帖子,它显示了Math.random()函数,但由于我对javascripting缺乏了解,所以我需要你的帮助才能使这个javascript随机工作,谢谢。

<script type='text/javascript'>
//<![CDATA[[
  function CompletedProject(json) {
    for (var i = 0; i < json.feed.entry.length; i++) {
      var judulPost = json.feed.entry[i].title.$t;
      var thumbPost = json.feed.entry[i].media$thumbnail.url;
      //var linkPost = json.feed.entry[i].link[1].href;
      var linkPost;  
    // Get rel=alternate for truly post url
    for (var j=0; j < json.feed.entry[i].link.length; j++)
    {
      if (json.feed.entry[i].link[j].rel == 'alternate')
      {
        linkPost = json.feed.entry[i].link[j].href;
        break;
      }
    }

      var showcompleted = '<div class="ani-item"><a href="'+linkPost+'"><img alt="'+judulPost+'" class="shine" data-original="'+thumbPost+'"/><h4>'+judulPost+'</h4></a></div>';
      document.write(showcompleted);
    }
  }
//]]>
</script>

  <script src='/feeds/posts/default/-/Complete?alt=json-in-script&amp;callback=CompletedProject'/>

1 个解决方案

#1


0  

var randomNumber = Math.floor(Math.random() * yourlabels.length);
yourlabels[randomNumber].yourmethod;

That's one way to use Math.random() to generate random integers for use as indexes. Here is a link to a project where I randomly generate a quote and randomly generate a background color with each click of the 'Load Quote' button. Find my Math.random() functions and see if you can incorporate them.

这是使用Math.random()生成随机整数以用作索引的一种方法。这是一个项目的链接,我随机生成一个引号,并在每次单击“加载引用”按钮时随机生成背景颜色。找到我的Math.random()函数,看看你是否可以合并它们。

https://github.com/KyleVassella/P1_RandomQuoteGenerator/blob/gh-pages/js/script.js

Math.floor() is a way to round down to the nearest whole number so that Math.random() can be used to represent an index value.

Math.floor()是一种向下舍入到最接近的整数的方法,以便Math.random()可用于表示索引值。

Note that my project also has that seen property which represents a boolean value and is attached to each one of my quote objects - this is used with a 'while' loop to prevent the same quote or background color from randomly generating more than once until each quote has been shown. You may or may not want this feature.

请注意,我的项目还有一个看到的属性,它表示一个布尔值,并附加到我的每个引用对象 - 这与'while'循环一起使用,以防止相同的引用或背景颜色随机生成多次,直到每个引用已被显示。您可能想要也可能不想要此功能。

#1


0  

var randomNumber = Math.floor(Math.random() * yourlabels.length);
yourlabels[randomNumber].yourmethod;

That's one way to use Math.random() to generate random integers for use as indexes. Here is a link to a project where I randomly generate a quote and randomly generate a background color with each click of the 'Load Quote' button. Find my Math.random() functions and see if you can incorporate them.

这是使用Math.random()生成随机整数以用作索引的一种方法。这是一个项目的链接,我随机生成一个引号,并在每次单击“加载引用”按钮时随机生成背景颜色。找到我的Math.random()函数,看看你是否可以合并它们。

https://github.com/KyleVassella/P1_RandomQuoteGenerator/blob/gh-pages/js/script.js

Math.floor() is a way to round down to the nearest whole number so that Math.random() can be used to represent an index value.

Math.floor()是一种向下舍入到最接近的整数的方法,以便Math.random()可用于表示索引值。

Note that my project also has that seen property which represents a boolean value and is attached to each one of my quote objects - this is used with a 'while' loop to prevent the same quote or background color from randomly generating more than once until each quote has been shown. You may or may not want this feature.

请注意,我的项目还有一个看到的属性,它表示一个布尔值,并附加到我的每个引用对象 - 这与'while'循环一起使用,以防止相同的引用或背景颜色随机生成多次,直到每个引用已被显示。您可能想要也可能不想要此功能。