相关疑难解决方法(0)

快速与容器内的大量元素进行交互(DOM,javascript)

所以我在容器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)

html javascript dom

9
推荐指数
3
解决办法
3460
查看次数

标签 统计

dom ×1

html ×1

javascript ×1