var placeholder=document.createElement("img");
placeholder.setAttribute("id","placeholder");
placeholder.setAttribute("src","images/placeholder.gif");
placeholder.setAttribute("alt","my image gallery");
var description=document.createElement("p");
description.setAttribute("id","description");
var desctext=document.createTextNode("choose an image");
description.appendChild(desctext);
document.body.appendChild(placeholder);
document.body.appendChild(description);

解决方案 »

  1.   

    如果直接放在 head 里肯定不行!至少有两种方法可以创建成功!方法一:script 加 defer
    <!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>
        <title> new document </title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="generator" content="editplus" />
        <meta name="author" content="[email protected]" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <script type="text/javascript" defer>
        <!--
    var placeholder=document.createElement("img");
    placeholder.setAttribute("id","placeholder");
    placeholder.setAttribute("src","images/placeholder.gif");
    placeholder.setAttribute("alt","my image gallery");
    var description=document.createElement("p");
    description.setAttribute("id","description");
    var desctext=document.createTextNode("choose an image");
    description.appendChild(desctext);
    document.body.appendChild(placeholder);
    document.body.appendChild(description);
        //-->
        </script>
    </head><body></body></html>
    方法二:放在 body 后面
    <!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>
        <title> new document </title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="generator" content="editplus" />
        <meta name="author" content="[email protected]" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
    </head><body></body>
        <script type="text/javascript" defer>
        <!--
    var placeholder=document.createElement("img");
    placeholder.setAttribute("id","placeholder");
    placeholder.setAttribute("src","images/placeholder.gif");
    placeholder.setAttribute("alt","my image gallery");
    var description=document.createElement("p");
    description.setAttribute("id","description");
    var desctext=document.createTextNode("choose an image");
    description.appendChild(desctext);
    document.body.appendChild(placeholder);
    document.body.appendChild(description);
        //-->
        </script>
    </html>
      

  2.   


    引入到外部的JS中,是不是按我说的方法弄,加个window.onload