<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function hello()
{
document.write(Math.random());
//alert("这是延迟后显示出来的!");
}
window.setInterval("hello()",2000);
</script>
</body>
</html>以上代码为什么无法在每2秒后执行document.write(Math.random());的效果
但是如果如下写
function hello()
{
//document.write(Math.random());
alert("这是延迟后显示出来的!");
}则可以达到每2秒后执行alert("这是延迟后显示出来的!");
的效果请问高手什么原因??HTML函数
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function hello()
{
document.write(Math.random());
//alert("这是延迟后显示出来的!");
}
window.setInterval("hello()",2000);
</script>
</body>
</html>以上代码为什么无法在每2秒后执行document.write(Math.random());的效果
但是如果如下写
function hello()
{
//document.write(Math.random());
alert("这是延迟后显示出来的!");
}则可以达到每2秒后执行alert("这是延迟后显示出来的!");
的效果请问高手什么原因??HTML函数
这时应该用.innerHTML或.innerText。
你的function全给清除了,所以效果停止
楼上说的对资料
http://blog.csdn.net/jxitc/article/details/6650403write 方法用于向文档流中写入内容。当文档在加载的时候,文档流是可写的,所以就不用调用open(),close()方法来打开和关闭输出流。当文档加载完毕过后,文档流是不可写的,要write内容就得先打开输出流,通过调用open()打开,这个open()方法则会清除当前文档(通常open()方法会在调用write时自动调用)。所以第一个问题:不是<p>html标签</p>无法加载,是清除了;同样的样式也清除了,当然你也得不head里的样式。
<script>
document.write("我是一些由document.write生成的字");//注意这里,浏览器在解释该行的时候,后面还有东西没加载完,这时候的 write 将不会导致 document 清空(因为 document 处于 open 状态)
</script>
<br>something there<br>
<input type="button" value="点我点我点我" onclick="document.write('尼玛居然全清了!')">而这里的 document.write,由于在点击时,整个html已经加载完了,document 已经处于 close 状态,如果你还是要写,那么 document.write 会自动重新 open 写入流,而 open 写入流的代价是,document全部清空
一旦所有的东西都加载完了,再调 document.write 就会导致清空
<body>
<script type="text/javascript">
function hello()
{
document.write(Math.random());
//alert("这是延迟后显示出来的!");
}
window.setInterval("hello()",2000);
</script>
</body>是每次都会显示随机数的,但是始终都是一个相同的数字,数字没有按照预想的每2秒钟重写下啊
<script type="text/javascript">
function hello()
{
document.getElementById("write").innerHTML = Math.random();
//alert("这是延迟后显示出来的!");
}
window.setInterval("hello()",2000);
</script><p id="write"></p>
</body>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
function hello()
{
document.write(Math.random());
//alert("这是延迟后显示出来的!");
}
window.setInterval("hello()",2000);
</script>
<body></body>
</html>这样
我想想,说的简单点:
1 如果浏览器在执行 document.write 的那一时刻,后面还有内容没加载完,则会在当前位置输出
例:
something here<br>
<script>
document.write("我是一些由document.write生成的字");
</script>
<br>something there<br>2 如果浏览器在执行 document.write 的那一时刻,整个页面已经加载完了,则会清空 body 中所有内容后输出
例:
something here<br>
<input type="button" value="点我点我点我" onclick="document.write('尼玛居然全清了!')">
当你点击这个按钮的时候,一定是页面整个加载完成后之后又经过几秒之后了。所以会全清了再看不懂的话,为楼主考虑,楼主还是考虑转行吧