日期:2014-05-17  浏览次数:20718 次

新手,问个基础的问题!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>使用有参函数</title>
<script type="text/javascript">
function showHello(count){
for(var i=0;i<count;i++){
document.write("<h2>Hello World</h2>");
}
}
</script>
</head>
<body>
<input name="btn" type="button" value="显示 5 次 HelloWorld" onclick="showHello(prompt('请输入显示Hello World的行数',''),'')" />
</body>
</html>

为什么我单击按钮输入一个数字后,title显示的是我文件所在的路径,单击后退才能恢复到原来的页面,调用函数后,函数为什么不在原来页面按钮下面显示?

------解决方案--------------------
document表示页面的内容,你的document.write("<h2>Hello World</h2>"); 相当于把当前页面的内容覆盖了,你按完按钮后,查看页面源代码,应该只有<h2>Hello World</h2><h2>Hello World</h2>。。。。。,这种格式了。
------解决方案--------------------
别用write
document.body.innerHTML="<h2>Hello World</h2>"
------解决方案--------------------
使用就jquery,append 你的html代码。不然就像楼上说的,把原页面覆盖了