如何在2秒钟内设置自动更改文本并在“字幕”中进行输出?



var greetings = [ "text1",
"text2",
"text3",
"text4",
"text5",
"text6",
"text7",
"text8",
"text9",
"text10"
];
var greeting_id = Math.floor(Math.random() * greetings.length);
document.getElementById('textme').innerHTML = greetings[greeting_id];

<div id="textme"></div>

最佳答案

marquee是一个过时的标记,仅少数浏览器可能支持。在代码中将div更改为marquee并每两秒钟使用setInterval更改文本



var greetings = ["hello",
  "ciao",
  "welcome",
  "howdy",
  "greetings",
  "salut",
  "hallo",
  "hola",
  "Gday",
  "Hey"
];
setInterval(function() {
  var greeting_id = Math.floor(Math.random() * greetings.length);
  document.getElementById('textme').innerHTML = greetings[greeting_id];
}, 2000);

<marquee id="textme"></marquee>

关于javascript - 使文本自动更改并随机化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45887070/

10-09 18:31