document.createDocumentFragment是个好方法
onclick事件执行不了,是因为你的字符串组织有问题var string = '<div style="background:red;width:300px;"><button onclick=test()>HelloWorld</button></div>';
onclick事件执行不了,是因为你的字符串组织有问题var string = '<div style="background:red;width:300px;"><button onclick=test()>HelloWorld</button></div>';
但是有什么方法解决使用大量createElement带来的麻烦吗?
</body>
<script language="JavaScript">
<!--
function test() {
alert('test');
}var string = "<div style='background:red;width:300px;'><button onclick='test()'>HelloWorld</button></div>";
window.onload=function(){
var div1 = document.getElementById("div1");
var txtNode = document.createDocumentFragment();
txtNode.appendChild(document.createElement("<div></div>"));
txtNode.firstChild.outerHTML = string;
div1.appendChild(txtNode); //div1.innerHTML = string;
}
//-->
</script>