使用JavaScript添加GIF

时间:2017-08-19 11:49:50

标签: javascript html css

我在JS中创建了一个按钮,可以为每个事实生成带有GIF或图像的随机事实(字符串),以使其更有趣。

我有一个小问题:照片/ GIF没有在我的网站上显示。



<img [src]="../../src/assets/images/1.jpg" alt="Ms dhoni" width="2000" height="2000"/>
&#13;
window.onload = () => {
    const factsArr = [
      {
        image: 'http://gph.is/153xv7h\250x300',
        content:"the horned lizard shoots out blood from its eyesockets when they feel danger is nearby"
      },
      {
        image: 'http://via.placeholder.com/200x100',
        content: "A Sesame Street episode (ep. 847) was aired on TV in 1976,it was so scary that the authorities had to pull it off due to several complaints from parents saying their children screamed in horror."
      }
    ];
  
    
    document.getElementById('generate-btn').addEventListener('click', () => {
      const idx = Math.floor(Math.random() * factsArr.length);
      document.getElementById('random-fact-content').innerHTML = factsArr[idx].content;
      document.getElementById('random-fact-image').setAttribute('src', factsArr[idx].image)
    })
      
}
&#13;
&#13;
&#13;

0 个答案:

没有答案