纯JS为DOM添加html字符串:appendHTML方法和prependHTML方法

 

 

appendHTML:

 

HTMLElement.prototype.appendHTML = function(html) {
    var divTemp = document.createElement("div"), nodes = null
        // 文档片段,一次性append,提高性能
        , fragment = document.createDocumentFragment();
    divTemp.innerHTML = html;
    nodes = divTemp.childNodes;
    for (var i=0, length=nodes.length; i 
   

 

prependHTML:

 

var prependHTML = function(el, html) {
    var divTemp = document.createElement("div"), nodes = null
        , fragment = document.createDocumentFragment();

    divTemp.innerHTML = html;
    nodes = divTemp.childNodes;
    for (var i=0, length=nodes.length; i

 

 

转自:http://www.zhangxinxu.com/wordpress/2013/05/js-dom-basic-useful-method/

你可能感兴趣的:(JS)