我有一个 Javascript 字符串列表,我想将其打印到 HTML div 中。这是 JavaScript 代码:
$(document).ready(function(){
list = ["One","Two","Three"];
for (var i = 0; i < list.length; i++){
$("#content").html(list[i]);
}
});
打印到 ID 为“content”的 HTML div 中。但当然只出现最后一个词。我想要的是控制每个 $("#content").html(list[i]); 之间的延迟这样我就可以依次看到列表中的每个单词。 Javascript 或 jQuery 有解决方案吗?
谢谢!
最佳答案
您可以使用setInterval
<强> Live Demo
$(document).ready(function(){
list = ["One","Two","Three"];
index = 0;
intr = setInterval(yourfun, 2000);
function yourfun()
{
$("#content").html(list[index++]);
if(index == 3) clearInterval(intr);
}
});
可以使用append
在前面添加内容,查看here .
关于javascript - 在 Javascript/jQuery 中延迟打印列表中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15945017/