代码如何,谢谢各位了。  
var img = document.createElement("img");
            img.style.margin = "0.8em 0";
            img.setAttribute("src", "img/20.gif");
            document.getElementById("msgDiv").appendChild(img);

解决方案 »

  1.   

    var img = document.createElement("img");
                img.style.margin = "10px 0";
                img.setAttribute("src", "img/20.gif");
                document.getElementById("msgDiv").appendChild(img); 
    试下,是不是0.8em的问题
      

  2.   

    chrome 里好像不能直接访问本地资源。
    比方你的html文件路径为:c:\workspac\test\demo.html 你直接双击(或者在地址栏输入此路径),在chrome里不能直接访问本地文件的(img/20.gif),必须是http路径才行。
      

  3.   


    用绝对地址试试~ http://开头的这种~
      

  4.   


    <html>
    <head>
    </head>
    <body>
    <div id='msgDiv'></div>
    <script>
    var img = document.createElement("img");
    img.style.margin = "0.8em 0";
    img.setAttribute("src", "img/20.gif");
    document.getElementById("msgDiv").appendChild(img); 
    </script>
    </body>
    </html>
    上面的代码在 IE8 和 Chrome 23 上面都可以运行。
      

  5.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function init(){
    var img = document.createElement("img");
                img.style.margin = "0.8em 0";
                img.setAttribute("src", "1.jpg");
                document.getElementById("msgDiv").appendChild(img); 
    }
    window.onload=init;
    </script>
    </head><body>
    <div id="msgDiv"></div>
    </body>
    </html>
    mei wen ti  a
      

  6.   

    会不会是CSS样式的问题按ctrl + shift + i 看看img元素有没有添加进html代码里