ie下innerHTML 不起作用的时候用的修复

参考来自 http://www.jonefox.com/blog/2009/05/21/internet-explorer-and-the-innerhtml-property/

代码

function replace_html(el, html) {
	if( el ) {
                var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
                var newEl = document.createElement(oldEl.nodeName);

                // Preserve any properties we care about (id and class in this example)
                newEl.id = oldEl.id;
                newEl.className = oldEl.className;

                //set the new HTML and insert back into the DOM
                newEl.innerHTML = html;
                if(oldEl.parentNode)
        	        oldEl.parentNode.replaceChild(newEl, oldEl);
                else
		        oldEl.innerHTML = html;

                //return a reference to the new element in case we need it
                return newEl;
	}
};

参考用法

replace_html(“div_id”,”<div>here is html data<div>”);

发表评论

邮箱地址不会被公开。 必填项已用*标注