Home >Web Front-end >JS Tutorial >Improve innerHTML performance through Dom methods_javascript tips

Improve innerHTML performance through Dom methods_javascript tips

WBOY
WBOYOriginal
2016-05-16 19:05:391004browse
Copy code The code is as follows:

function replaceHtml(el, html) {
var oldEl = typeof el === "string" ? document.getElementById(el) : el;
/*@cc_on // The original innerHTML performs better in IE
oldEl.innerHTML = html;
return oldEl ;
@*/
var newEl = oldEl.cloneNode(false);
newEl.innerHTML = html;
oldEl.parentNode.replaceChild(newEl, oldEl);
/* Once we Remove the old element from the DOM and return the new element reference. */
return newEl;
};

There is still performance improvement for Opera, but the improvement is not as amazing as the above two browsers.
Only in IE, The original innerHTML method is more efficient.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn