<body onload="view()">
<script language="javascript">
function show(a)
{
alert(a);
}function view()
{
b=2;
document.write('<input type="button" value="test" onclick="show('+b+')" />');
}
</script>
</body>
<script language="javascript">
function show(a)
{
alert(a);
}function view()
{
b=2;
document.write('<input type="button" value="test" onclick="show('+b+')" />');
}
</script>
</body>
<script language="javascript">
function show(a)
{
alert(a);
}document.write('<input type="button" value="test" onclick="show(2)" />');
</script>
</body>
能使用document.write的形式,只能是像下面这样,不由事件来触发。
<script language="javascript" type="text/javascript">
document.write("ok");//或者这样
function WriteSomething(){
document.write("something");
}
//然后直接调用,而不是事件调用
WriteSomething();
</script>
<head>
<title></title>
<script language="javascript" type="text/javascript">
function WriteSomething(){
document.write("这是document.write之后的页面,请再右键查看源文件,看只剩下什么?");
}
</script>
</head>
<body>
这是由事件触发document.write之前的内容,请右键查看源文件<br/>
<input type="button" value="调用document.write" onclick="WriteSomething();" />
</body>
</html>
onload以后是不可以写入document的。
直接在html里面写document.write是在解析html的时候写入,没问题。
但是onload是document执行完毕之后触发,会覆盖先前的文档。