在添加元素之前,你首先要确定要把你创建的新元素添加到那个元素里,appendChild(newChild)  将newChild添加到运行该函数的节点之中(末端)

解决方案 »

  1.   

    ie还需要加个tbody,var tbody  = document.createElement("tbody");
    td.appendChild(txt); 
    tr.appendChild(td); 
    atr.nodeValue="10"; 
    atr2.nodeValue="display:block"; 
    table.setAttribute(atr); 
    table.setAttribute(atr2); 
    tbody.appendChild(tr);
    table.appendChild(tbody); 
    document.body.appendChild(table);
      

  2.   

    高手真多啊 谢谢~  不过下面代码:
    var tbody  = document.createElement("tbody"); 
    td.appendChild(txt);  
    tr.appendChild(td);  
    atr.nodeValue="10";  
    atr2.nodeValue="display:block";  
    table.setAttribute(atr);  
    table.setAttribute(atr2);  
    tbody.appendChild(tr); 
    table.appendChild(tbody);  document.body.appendChild(table);执行后,为啥 tbale显示无边框啊?
      

  3.   

    完整代码如下:
    function b()
    {

    var table=document.createElement("table");
    var tr=document.createElement("tr");
    var td=document.createElement("td");
    var txt=document.createTextNode("aa");
    var atr=document.createAttribute("border");
    var atr2=document.createAttribute("style");
    var tbody  = document.createElement("tbody"); td.appendChild(txt);
    tr.appendChild(td);
    atr.nodeValue="1";
    atr2.nodeValue="display:block";
    table.setAttribute(atr);
    table.setAttribute(atr2);
    tbody.appendChild(tr);
    table.appendChild(tbody);document.body.appendChild(table);
    }
    请求DOM高手赐教啊~!
      

  4.   

    setAttribute对ie貌似无效,用style.的形式试试
      

  5.   

    用这个方法吧
    Element.setAttribute(name,value)用在你的例子中就是:table.setAttribute("border","1px");
    table.setAttribute("style","color:red");.....等等等等good luck
      

  6.   

    setAttribute对IE大部分有效
    但是如果属性是事件和样式等在IE里无法用setAttribute
    也就是
    样式:
    obj.setAttribute("style", "color:red")
    在FF里有效,在IE里无效
    解决方法:obj.style.color = "red";
    或者obj.style.cssText = "color:red";事件如:
    obj.setAttribute("onclick", "a()");
    在FF里有效,在IE里无效
    解决方法:
    //此写法要注意内存泄露
    obj.onclick = function() {alert(1)}所以通常为了兼容的话一般都不用setAttribute
    直接对属性赋值。如obj.a = "aa"
      

  7.   

    obj.setAttribute("onclick", "a()"); 
    在FF里有效,在IE里无效 
    解决方法: 
    //此写法要注意内存泄露 
    obj.onclick = function() {alert(1)}