插入操作时的一个特殊需求,如果此节点没有加入DOM树就克隆一份,否则就直接移动节点!

      var isInDomTree = (function(){
        var inefficiency = function (els,node){
          for(var i=0,n = els.length;i<n;i++){
            if(els[i] === node){
              return true
            }
            if(els[i] && els[i].childNodes.length > 0){
              var e = inefficiency(els[i].childNodes,node);
              if(e) return e;
            }
          }
          return false
        },
        root = document.documentElement;
        return root.compareDocumentPosition ? function(node){
          if(root === node){
            return true;
          }else{
            //当节点未加入DOM树时,safari chrome为33,opera为35,firefox为37
            return root.compareDocumentPosition(node) < 33
          }
        }:function(node){
          if(node.nodeType === 1){
            return root.contains(node);//相当或包含为true,但必须两者为元素节点
          }else{
            return inefficiency([root],node);
          }
        }
      })();

//2010 .4. 13新修订
      var isInDomTree = function(node,context){
            var root = context.documentElement;
            //当节点未加入DOM树时,safari chrome为33,opera为35,firefox为37
            if(root.compareDocumentPosition)
                return root === node || (node.compareDocumentPosition(root) & 8) === 8;
            //相等或包含为true,但必须两者为元素节点
            if(root.contains && node.nodeType === 1)
                return  root.contains(node)
            while ((node = node.parentNode))
                if (node === root) return true;
            return false
        }

相关文章:

  • 2021-12-05
  • 2022-12-23
  • 2022-02-26
  • 2021-08-07
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-13
  • 2022-12-23
  • 2021-07-21
  • 2022-12-23
  • 2021-11-16
相关资源
相似解决方案