<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>

解决方案 »

  1.   

    <body>
    <script language="javascript">
    function show(a)
    {
       alert(a);
    }document.write('<input type="button" value="test" onclick="show(2)" />');
    </script>
    </body>
      

  2.   

    我这里测试你这个是没有问题的你把脚本放到body标签前看看
      

  3.   

    问题的根本在于:<body onload="view()">和view()中的document.write因为当onload的事件被触发的时候,document文档已经关闭,不能再write了。然而此时再重新write的后果就是,document的内容被完全清空,而剩下的内容就是最新write的东西,之前的东西会一点都不会剩下所以要牢记一点,不要在任何事件中,使用document.write,否则你的页面就会只剩下你write的东西
    能使用document.write的形式,只能是像下面这样,不由事件来触发。
    <script language="javascript" type="text/javascript">
    document.write("ok");//或者这样
    function WriteSomething(){
        document.write("something");
    }
    //然后直接调用,而不是事件调用
    WriteSomething();
    </script>
      

  4.   

    上一段代码说明问题:<html>
        <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>
      

  5.   

    #4 #5楼是正确答案。
    onload以后是不可以写入document的。
    直接在html里面写document.write是在解析html的时候写入,没问题。
    但是onload是document执行完毕之后触发,会覆盖先前的文档。