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);<img id="placeholder" src="placeholder.gif" alt="my"/>
<p id="description">choose an image</p>
setAttribute属性不是更改现有的属性节点嘛,为什么可以连续创建属性节点,和属性节点中的内容,不是很理解

解决方案 »

  1.   

    这个只是设置属性,不是创建属性节点.
    placeholder.setAttribute("id","placeholder");
    等于
    placeholder.id="placeholder";(id不是自定义属性)
      

  2.   

    解释得不错,如果你试图写个其他非系统的属性比如
    placeholder.setAttribute("cscs","placeholder"); //这句就会错了,系统没有这个属性!
    但是如果自己写代码却是正常可以获取!
      

  3.   

    测试了吗?
    你试试下面的代码
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload=function(){
    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");
    description.setAttribute("newAttr","Iamnewer")
    var desctext=document.createTextNode("choose an image");
    description.appendChild(desctext);
    document.body.appendChild(description);
    document.body.appendChild(placeholder);
    };
    </script>
    <style type="text/css">
    #div {
    width:450px;
    height:450px;
    }
    #div a{
    position:absolute;
    }
    </style>
    </head>
    <body>
    <input type="button" id="test" onclick="alert(document.getElementById('description').getAttribute('newAttr'))" value="test" />
    </body>
    </html>
      

  4.   

    另外 关于属性我想起了数组的一个问题
    var i = [];
    i[3] = 5;//此时i.length = 4很容易得到
    i["test"] = 3;//i.length=4
    i["5"] = 9; //i.length =6
    var j = [];
    j["test"] = 11;//j.length = 0;同时
    var k = {};
    k["test"] = 5;//k.length = undefined没什么难理解的
    但是值得小心~~~~!
      

  5.   


    description.setAttribute("newAttr","Iamnewer")也就是说可以自定义任何属性节点名称和属性值????