当前位置: 首页 > 工具软件 > JsHtml > 使用案例 >

JS实现appendHTML

郦楷
2023-12-01
思路一:
function appendHtml(elem,value){
    var node = document.createElement("div"),
        fragment = document.createDocumentFragment(),
        childs = null,
        i = 0;
    node.innerHTML = value;
    childs = node.childNodes;
    for( ; i < childs.length; i++){
        fragment.appendChild(childs[i]);
    }
    elem.appendChild(fragment);
    childs = null;
    fragment = null;
    node = null;
}
思路二: element.insertAdjacentHTML(position, html);
document.body.insertAdjacentHTML("beforeend","<h3>你好</h3>")

说明:position是相对于element元素的位置(beforebegin、afterbegin、beforeend、afterend)

 类似资料: