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

Improve innerHTML performance through Dom methods_javascript tips

WBOY
Release: 2016-05-16 19:05:39
Original
1015 people have browsed it
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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template