用a=document.createElement(“div”) 
a.setAttribute("id","aid");创建元素后,是否可以通过document.getElementById("aid")对他进行访问? 
==========完全可以
remove的时候是否自动删除,还是需要再display:none一下? 
==========不需要,remove后就不显示了
如果对a.setAttribute("class","classname")后,能否设置样式.classname{...}? 
==========ie不支持这样ff支持这样,不过两个都支持a.className="xxxx";
能否直接适用a.setAttribute("style","width:200px")之类的方法设置样式,我试验的不行,可否有其他办法?
==========ie不支持这样ff支持这样,ie支持a.style.cssText = 'color:red;'最好的办法是两种方法都用上

解决方案 »

  1.   

    楼上的很强悍、佩服。
    我设置了a.setAttribute("name","myname")后,用
    document.getElementsByName("myname").length查找我创建的div个数,怎么只能找到一个。
    莫非这个name跟class一样ie下也不好用
      

  2.   

    下面这段代码在FF下length是2, IE下是0
    如果把name换成id,则IE下是2,FF下是0<style>
    .myclass { 
    text-align:left; 
    width:315px; 
    height:100px; 
    border: 1px solid #333333; 
    background:#FFFFCC; 
    padding:2px; 

    </style>
    <body>
    <div id="left_div1" style="border:#0066CC 1px solid; height:500; width:500;"></div><script>
    //new div 
    var rss_div1=document.createElement("div"); 
    //rss_div1.setAttribute("class","myclass"); 
    rss_div1.className="myclass"; 
    rss_div1.setAttribute("name","myname"); 
    rss_div1.innerHTML="the string11"; var rss_div2=document.createElement("div"); 
    //rss_div1.setAttribute("class","myclass"); 
    rss_div2.className="myclass"; 
    rss_div2.setAttribute("name","myname"); 
    rss_div2.innerHTML="the string22"; 
    //get father_div 
    var father_div=document.getElementById("left_div1"); 
    //add 
    father_div.appendChild(rss_div1); 
    father_div.appendChild(rss_div2); var list=document.getElementsByName("myname");
    alert(list.length);</script>
    </body>
      

  3.   

    不行 不能设置创建div 的 id
    只能设置他的name 属性
    也不能通过document.getElementById("aid") 来访问因为他更本不存在源文件里面
    document 是获取 页面文件中的 remove 直接就删除了这个节点不用display 是隐藏 但是实际还是存在的
    设置样式用
    a.style.width="200px";
      

  4.   

    document.createElement(“div”) 
    a.setAttribute("id","aid");创建元素后,是否可以通过document.getElementById("aid")对他进行访问? 这个是不能通过document.getElementById()进行访问的,这样创建的元素不属于文档的一部分,不能通过getElementById得到
      

  5.   

    再次求助。。解决了这个问题可以加分
    奇怪了,我动态创建的时候,可以根据id得到div
    (IE7浏览器)下,在一个js下:
    单击后响应下面这个函数
    function new_module()
    {
    var temp_div=document.createElement("div");
    temp_div.setAttribute("id","temp_div");
    var header=document.getElementById("header");
    header.appendChild(temp_div);
            temp_div.innerHTML="<input type='button' value='确认' onclick='create()'></input>";
    }
    弹出div层后,点确认调用create
    function create()
    {
    ...
    建立n多层【这个过程中,我建立好层后根据getElementById却一个层也得不到。】?????
    ...
    //最后删除temp_div层
    document.getElementById("header").removeChild(document.getElementById("temp_div"));//这行就是删除,确实是那个层就没有了?????
    }