var tdiv=document.createElement("div");
    tdiv.id="myhd";
    var ipt=document.getElementById("myhd");
         alert(ipt)  //打印出来的是null,为什么

解决方案 »

  1.   

    <script type="text/javascript">
        var tdiv = document.createElement("div");
        tdiv.id = "myhd";
        var ipt = document.getElementById("myhd");
        alert(tdiv);
        document.body.appendChild(tdiv);
        alert(tdiv);
    </script>
      

  2.   

    lz是没有把这个element append 到 document里 所有是null
    而你append到了body里面 所以能打印出来~
      

  3.   

       楼主,这样来处理就可以了;
     var tdiv= document.createElement("div");
        //给当前的DIV 添加id为moveid
        movediv.setAttribute("id", "myhd");
        //添加到body里面
        document.body.appendChild(tdiv);//然后就可以操作这个div了
        document.getElementById("myhd").value=1;
      alert(document.getElementById("myhd").value)