<script type="text/javascript">
    document.write("<div>div1</div>");
    function b(){
        document.write("<div>div2</div>");
    }
</script>
<input type="button" value="ok" onclick="b();" />上面这段代码点击ok后,页面只显示div2,我想在点击ok后页面显示的是:
<div>div1</div>
<div>div2</div>

解决方案 »

  1.   

    lz?document.write(" <div>div1 </div>"); 直接放里面
      

  2.   

    <script type="text/javascript">    function b(){
            document.write(" <div>div1 </div>");
            document.write(" <div>div2 </div>");
        }
    </script>
    <input type="button" value="ok" onclick="b();" />
      

  3.   

    onload以后的document.write会刷新文档流,所以不能使用这个方法document.body.innerHTML+="<div>div2 </div>";
    或者你可以添加后某个元素里
    documentgetElementById("elementID").innerHTML+="<div>div2 </div>";
      

  4.   

    function b(){ 
            document.write(" <div>div2 </div>"); 
        } <input type="button" value="ok" onclick="b();" /> 点击按钮后 会创建一个新的输出流 ,原先输出流的内容会被冲掉。新的输出流内容是 document.write 的内容 所以你看到的只是一个<div>div2 </div>可以设置body 的 innerHTML 也可以在 页面底部放一容器元素 设置它的 innerHTMl具体代码可参照楼上
      

  5.   

    innerHTML是可以,但是用innerHTML,比如innerHTML += "<div id='div2'>div2</div>";
    执行之后使用
    getElementById("div2")取不到这个对象
      

  6.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
     </HEAD> <BODY>
      <script type="text/javascript"> 
            document.write(" <div>div1 </div>"); 
        function b(){         document.body.innerHTML+="<div id='div2'>div2 </div>";


        } 
    function setValue()
    {
           document.getElementById('div2').innerText=document.getElementById('txt').value;
    }
    </script> 
    <input type="button" value="ok" onclick="b();" /><br>
    <input type='text' id='txt' />
    <input type="button" value='set div2 value' onclick='setValue();' />
     </BODY>
    </HTML>