本文介绍了setinterval无法运行document.writeln的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问候,
以下代码适用于警报,但不适用于document.writeln。是因为onload事件吗?如果是这样,浏览器在完成加载后是否应该能够添加更多元素? - 试图理解它是如何工作的(我在此期间研究jquery)。
The code below works for alert but doesn't for document.writeln. Is it because of the onload event? If so shouldn't the browser be able to add more elements after it's finished loading? - Trying to understand how it works (I am studying jquery in the meantime).
<body onload="timeMsg()">
<script type="text/javascript">
function timeMsg()
{
var t=self.setInterval("randInt()",3000);
}
function randInt()
{
document.writeln("<br />" +Math.floor(Math.random() * 7)); //doesn't work
alert(Math.floor(Math.random() * 7)); //works
}
</script>
</body>
推荐答案
替代
<body onload="timeMsg()">
<div id="outputDiv"></div>
<script type="text/javascript">
function timeMsg()
{
var t = self.setInterval("randInt()", 3000);
}
function randInt()
{
document.getElementById("outputDiv").innerHTML += "<br />" + Math.floor(Math.random() * 7);
alert(Math.floor(Math.random() * 7)); //works
}
</script>
这篇关于setinterval无法运行document.writeln的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!