所以我在容器div中有大量的div(4000-5000)[每个包含跨度,锚点,图像等],基本上我将它们的显示设置为none或基于条件阻止.这确实需要一些时间.
在我搜索更快的内容时,我遇到了这个页面https://developers.google.com/speed/articles/javascript-dom,解决方法是从DOM中删除容器div并通过getElementsByTagName迭代包含的元素.
/**
* Remove an element and provide a function that inserts it into its original position
* @param element {Element} The element to be temporarily removed
* @return {Function} A function that inserts the element into its original position
**/
function removeToInsertLater(element) {
var parentNode = element.parentNode;
var nextSibling = element.nextSibling;
parentNode.removeChild(element);
return function() {
if (nextSibling) {
parentNode.insertBefore(element, nextSibling);
} else {
parentNode.appendChild(element);
}
};
}
function updateAllAnchors(element, anchorClass) {
var insertFunction = removeToInsertLater(element);
var …Run Code Online (Sandbox Code Playgroud)