如何设置 自动更改文字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>
&#13;
答案 0 :(得分:0)
您可以使用 setInternal 功能:
https://www.w3schools.com/jsref/met_win_setinterval.asp
这样:
setInterval(changeText, 2000);
function changeText()
{
var greeting_id = Math.floor(Math.random() * greetings.length);
document.getElementById('textme').innerHTML = greetings[greeting_id];
}
如您所见,这将每隔 2秒或2000毫秒调用该函数。
对于选框,你可以放一个选框,然后你的div:
https://www.tutorialspoint.com/html/html_marquee_tag.htm
这样:
<marquee><div id="textme"></div></marquee>
最终结果:
var greetings = [ "text1",
"text2",
"text3",
"text4",
"text5",
"text6",
"text7",
"text8",
"text9",
"text10"
];
//Maybe this is clearer for you...
setInterval(changeText, 2000);
function changeText()
{
var greeting_id = Math.floor(Math.random() * greetings.length);
document.getElementById('textme').innerHTML = greetings[greeting_id];
}
//But you also can use:
/*
setInterval(function () {
}, 2000);
*/
//As you can see in other answers. :)
<marquee><div id="textme"></div></marquee>
最后一条建议,字幕是一个过时的标记,所以我建议您这样做:CSS3 Marquee Effect
如果你想在每个循环中显示不同的文字,你可能正在寻找一个滑块。
此外,您可以更改选取框间隔,建议不要这样做,因为在长屏幕中,文字难以阅读:
http://www.plus2net.com/html_tutorial/html_marquee_speed.php
我已经为你做了这个:
var greetings = [ "text1",
"text2",
"text3",
"text4",
"text5",
"text6",
"text7",
"text8",
"text9",
"text10"
];
var msecs = 2000; //Milliseconds that will wait to change the text
var marq = document.getElementById("marq"); //The object
var pix = marq.clientWidth; //The width in the screen
//You have to vary this... If you know a little bit of maths they can help you.
marq.scrollAmount = (pix / (msecs / 50));
setInterval(function()
{
var greeting_id = Math.floor(Math.random() * greetings.length);
document.getElementById('textme').innerHTML = greetings[greeting_id];
}, msecs);
<marquee id="marq"><div id="textme"></div></marquee>
答案 1 :(得分:0)
marquee
是一个过时的标记,只有少数浏览器可以支持。将代码中的div
更改为marquee
&amp;每两秒钟使用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);
&#13;
<marquee id="textme"></marquee>
&#13;
答案 2 :(得分:0)
尝试这样的事情:
var greetings = [ "text1",
"text2",
"text3",
"text4",
"text5",
"text6",
"text7",
"text8",
"text9",
"text10"
];
var greeting_id = Math.floor(Math.random() * greetings.length);
document.getElementById('textme').textContent = greetings[greeting_id];
setInterval(function(){
var greeting_id = Math.floor(Math.random() * greetings.length);
document.getElementById('textme').textContent = greetings[greeting_id];
}, 3000);
<marquee id="textme">Change text between marquee tags</marquee>
答案 3 :(得分:0)
<强> HTML 强>
<marquee><div id="textme"></div></marquee>
<强> JAVASCRIPT 强>
var greetings = [ "text1",
"text2",
"text3",
"text4",
"text5",
"text6",
"text7",
"text8",
"text9",
"text10"
];
setInterval(()=> {
var greeting_id = Math.floor(Math.random() * greetings.length);
document.getElementById('textme').innerHTML = greetings[greeting_id];
}, 2000)
如果您不想使用选取框功能,但只想更改常规间隔的文字,请从HTML中删除选框标记。
答案 4 :(得分:0)
var greetings = [ "text1",
"text2",
"text3",
"text4",
"text5",
"text6",
"text7",
"text8",
"text9",
"text10"
];
//To change the text after 2 seconds
setTimeout(function(){
var text = greetings[Math.floor(Math.random()*greetings.length)];
document.getElementById('textme').innerHTML = text;
},2000);
//To change the text every 2 seconds
setInterval(function(){
var text = greetings[Math.floor(Math.random()*greetings.length)];
document.getElementById('textme2').innerHTML = text;
},2000);
<div id="textme"></div>
<div id="textme2"></div>