body的操作必须要放在window.onload里<script type="text/javascript">
var str   =   "" 
    +  " <div   id='smart'"
    +  " style='" 
    +  "width:800px;" 
    +  "height:300px;" 
    +  "'" 
    +  ">" 
    + "DIV内的文字" 
    + "</div>"
window.onload = function () {
document.body.insertAdjacentHTML("afterEnd", str)
};
</script>

解决方案 »

  1.   

    楼上说明了
    必须是在你页面加载时操作BODY
      

  2.   

    我建议你这样做:
    var div = document.createElement("div");
    div.id = "div1";
    div.style.width = 800;
    div.style.height = 300;
    div.innerText = "DIV内的文字" ;document.appendChild(div);
    要找这个div,就 var div = document.getElementById("div1");
      

  3.   

    <html>
    <head>
    </head>
    <script>
    function startDrag(obj)
    {
    alert("dddd");
    }function setConfig()
    {
        var str   =   "" 
       + "<div id=message "
       + "style="
        + "width:50;"
       + "height:60;"
       + "background-color:lightyellow;"
       + "onmousedown='startDrag(this)' "
       + ">"
        +"ddd"
       +"</div>"
       
    document.body.insertAdjacentHTML("beforeEnd", str);  }
    </script>
    <body>
    <input type="button" text="value"  value="提交"  onClick="setConfig();">
    </body>
    </html>
    我现在加载DIV成功了,可是为什么加载的事件onmousedown='startDrag(this)没有触发
      

  4.   

    <html>
    <head>
    </head>
    <script>
    function startDrag(obj)
    {
    alert("dddd");
    }function setConfig()
    {
        var str   =   "" 
       + "<div id=message "
       + "style="
        + "width:50;"
       + "height:60;"
       + "background-color:lightyellow;"
       + "onmousedown='startDrag(this)' "
       + ">"
        +"ddd"
       +"</div>"
       
    document.body.insertAdjacentHTML("beforeEnd", str);  }
    </script>
    <body>
    <input type="button" text="value"  value="提交"  onClick="setConfig();">
    </body>
    </html>
    我现在加载DIV成功了,可是为什么加载的事件onmousedown='startDrag(this)没有触发,这是为什么,有什么办法?
      

  5.   

    因为startDrag写在setConfig函数体里 所以只有点击提交按扭才能触发
      

  6.   

    var   div   =   document.getElementById( "div1 "); 
    div.attachEvent("onmousedown",startDrag);
      

  7.   

    已经和你说过了是因为页面未载完的问题怎么不看?
    闪了。。
    <script type="text/javascript">
    function startDrag(obj) {
    alert("dddd");
    }var div = [
       '<div id="smart" style="width:800px;height:300px;"'
       , ' onmousedown="startDrag(this)"'
       , '>'
       , 'DIV内的文字'
       , "</div>"
    ].join("");window.onload = function () {
    document.body.insertAdjacentHTML("beforeEnd", div);
    }
    </script>
      

  8.   

    楼上的大哥,不好意思,我是新手,太多不懂了,不过我这个代码要求 不能在加载LOAD的时候加上DIV,必须要事件触发(button),看的不是太懂,有谁解释下?7楼的大哥,我按照你的写法去弄了,可是还是有错误?有谁赐教下,感谢!!!!
      

  9.   

    <html> 
    <head> 
    </head> 
    <script> 
    function  setConfig() 

            var   str   =" "   
          +   "<div  id=message" 
          +   "style= " 
           +   "width:50;" 
          +   "height:60;" 
          +   "background-color:lightyellow;" 
          +   " onmouseover='startDrag()'"
          +   ">" 
          + "ddd" 
          + " </div>" 
     
          
    document.body.insertAdjacentHTML("beforeEnd",str);     } 
     function  startDrag() 
          { 
            alert("dddd"); 
          } 
    </script> 
    <body> 
    <input  type= "button"   text= "value"     value= "採岎"     onClick="setConfig()"> 
    </body> 
    </html> 
    通过测试 注意在onmouseover前面加个空格!!解决及时结帖谢谢!~
      

  10.   


    <script type="text/javascript">(function (bool) {
    if (bool) {
    HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
    //BlueDestiny
    var df; var r = this.ownerDocument.createRange();
    switch (String(sWhere).toLowerCase()) {
    case "beforebegin":
    r.setStartBefore(this);
    df = r.createContextualFragment(sHTML);
    this.parentNode.insertBefore(df, this);
    break;
    case "afterbegin":
    r.selectNodeContents(this);
    r.collapse(true);
    df = r.createContextualFragment(sHTML);
    this.insertBefore(df, this.firstChild);
    break;
    case "beforeend":
    r.selectNodeContents(this);
    r.collapse(false);
    df = r.createContextualFragment(sHTML);
    this.appendChild(df);
    break;
    case "afterend":
    r.setStartAfter(this);
    df = r.createContextualFragment(sHTML);
    this.parentNode.insertBefore(df, this.nextSibling);
    break;
    }
    };
    }
    })(/Firefox/.test(window.navigator.userAgent));function startDrag(obj) {
    alert("dddd");
    }var div = [
       '<div id="smart" style="width:800px;height:200px;'
       , 'border:#CCCCCC 1px solid;background-color:#F4F4F4;"'
       , ' onmousedown="startDrag(this)"'
       , '>'
       , 'DIV内的文字'
       , "</div>"
    ].join("");var addDiv = function (id) {
    if (!document.getElementById(id)) document.body.insertAdjacentHTML('beforeEnd', div)
    };
    </script>
    <button onclick="addDiv('smart')">添加</button>
      

  11.   

    谢谢了,太马虎了我
    <script> 
    var oDiv = document.createElement("DIV"); 
    oDiv.id = "shop01"; 
    oDiv.style.top = 200; 
    oDiv.style.left = 200; 
    oDiv.style.background = '#FFFF00'; 
    oDiv.style.visibility = 'visible'; 
    oDiv.innerHTML="123123" 
    document.body.appendChild(oDiv); 
    oDiv.onclick = abc; 
    function abc(){ 
    alert("abc"); 

    </script>
    这样也是一个道理。MARK
      

  12.   

    为了灵活,改下
    <script type="text/javascript">(function (bool) {
    if (bool) {
    HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
    //BlueDestiny
    var df; var r = this.ownerDocument.createRange();
    switch (String(sWhere).toLowerCase()) {
    case "beforebegin":
    r.setStartBefore(this);
    df = r.createContextualFragment(sHTML);
    this.parentNode.insertBefore(df, this);
    break;
    case "afterbegin":
    r.selectNodeContents(this);
    r.collapse(true);
    df = r.createContextualFragment(sHTML);
    this.insertBefore(df, this.firstChild);
    break;
    case "beforeend":
    r.selectNodeContents(this);
    r.collapse(false);
    df = r.createContextualFragment(sHTML);
    this.appendChild(df);
    break;
    case "afterend":
    r.setStartAfter(this);
    df = r.createContextualFragment(sHTML);
    this.parentNode.insertBefore(df, this.nextSibling);
    break;
    }
    };
    }
    })(/Firefox/.test(window.navigator.userAgent));var startDrag = function (obj) {
    alert("dddd");
    }var addDiv = function (id) {
    var div = [
       '<div id="' + id + '" style="width:800px;height:200px;'
       , 'border:#CCCCCC 1px solid;background-color:#F4F4F4;"'
       , ' onmousedown="startDrag(this)"'
       , '>'
       , 'DIV内的文字'
       , "</div>"
    ].join("");
    if (!document.getElementById(id)) document.body.insertAdjacentHTML('beforeEnd', div);
    };
    </script>
    <button onclick="addDiv('smart')">添加</button>
      

  13.   

    没用createElement的原因你可以参考这个http://topic.csdn.net/u/20071109/09/eae5ded3-54ab-4138-b1d0-d3507ceca708.html在IE里createElement后的资源是永久存在的(温柔善良的月影姐姐说的。。)