日期:2014-05-16  浏览次数:20441 次

js创建节点的通用方法 研读
function make(tagname,attributes,children){
//如果参数的个数为二个,并且属性的类型是数组或是字符串,将当前的属性值赋给孩子
if(arguments.length == 2 && 
(attributes instanceof Array || typeof attributes == "string")){
children = attributes;
attributes = null;
}
//创建了一个元素
var e = document.createElement(tagname);
//为新标签设置属性
if(attributes) {
for(var name in attributes)
e.setAttribute(name, attributes[name]);
}

if(children != null){
if(children instanceof Array){
for(var i = 0; i < children.length; i++){
var child = children[i];
if(typeof child == "string"){
child = document.createTextNode(child);
e.appendChild(child);
}
}
}
else if(typeof children == "string"){
e.appendChild(document.createTextNode(children));
}
else e.appendChild(children);
}

return e;

}
以上是js部分,<button onclick="make('table',{border:1},'tr');">click</button>是body里,现在是怎么让新创建的表格显示在指定位置?还有判断Array和string有什么不同,是通过他们去判断什么吗?

------解决方案--------------------
我总结了一下http://blog.csdn.net/kunkkacoco/article/details/7755397