扫码关注官方订阅号
appendchild只能节点,有没有直接拼html代码的方法呢?
jjj ddd
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
<script type="text/javascript"> function append(parent, text) { if (typeof text === 'string') { var temp = document.createElement('p'); temp.innerHTML = text; // 防止元素太多 进行提速 var frag = document.createDocumentFragment(); while (temp.firstChild) { frag.appendChild(temp.firstChild); } parent.appendChild(frag); } else { parent.appendChild(text); } } window.onload = function() { var text = '<h4>Welcome!</h4><ul><li>A</li><li>B</li><li>C</li></ul><p>Hello, World</p>'; append(document.body, text); }; </script>
补充一句,可以不使用fragment,使用原因解释在这:http://mdn.beonex.com/en/DOM/document.createDocumentFragment.html
没听说过哦,印象中jQuery内部的实现是用innerHTML的方式实现的append一个字符串的。
jQuery
innerHTML
append
所以如果你追求原生,你也可以innerHTML。
或者我low了
insertAdjacentHTML
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
补充一句,可以不使用fragment,使用原因解释在这:http://mdn.beonex.com/en/DOM/document.createDocumentFragment.html
没听说过哦,印象中
jQuery内部的实现是用innerHTML的方式实现的append一个字符串的。所以如果你追求原生,你也可以
innerHTML。或者我low了
insertAdjacentHTML