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高手赐教啊~!
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);
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显示无边框啊?
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高手赐教啊~!
Element.setAttribute(name,value)用在你的例子中就是:table.setAttribute("border","1px");
table.setAttribute("style","color:red");.....等等等等good luck
但是如果属性是事件和样式等在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"
在FF里有效,在IE里无效
解决方法:
//此写法要注意内存泄露
obj.onclick = function() {alert(1)}