一个简单的创建dom的函数
var regName = /^(div|a|p|ul|li|input|select|document|body|iframe)$/;
function createDom(name, obj) { var match = regName.exec(name), dom; if (match && match[1]) { dom = document.createElement(match[1]); } if (dom.nodeType && dom.nodeType == 1) { for (var i in obj) { var match = i.split(/\./); var len = match && match.length || 0; if (len == 1) { dom[match[0]] = obj[i]; } else if (len == 2) { dom.style[match[1]] = obj[i]; } } } return dom; }
调用方法示例
var newdom = createDom("input", { "className": "alertInput data", "type": "text", "value":"123", "style.width":"100px" });
createDom("div", { "className": "_div", "id": "test", "style.cssText": "margin:0;padding:0;position:fixed;top:0;left:0" })
生活总是催促我们向前!