国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

javascript - Why does parent have to be written in front of the insertBefore function, but it is not needed when applied to other functions?
為情所困
為情所困 2017-05-19 10:24:28
0
1
1113
function insertAfter(newElement,targetElment) {
    var parent = targetElment.parentNode;
    if(parent.lastChild == targetElment) {
        parent.appendChild(newElement); 
    } else {
        parent.insertBefore(newElement,targetElment.nextSibling);
    }
}
為情所困
為情所困

reply all(1)
習(xí)慣沉默

Because the DOM Node does not have this method, if you want to use it this way, just implant the method into the Node prototype

Node.prototype.insertAfter = function insertAfter(newNode, targetNode) {
  if (!(this instanceof Node)) {
    throw new TypeError('Illegal invocation')
  }
  
  if (arguments.length < 2) {
    throw new TypeError("Failed to execute 'insertAfter' on 'Node': 2 arguments required, but only " + arguments.length +" present.")
  }
  
  if (!(newNode instanceof Node)) {
    throw new TypeError("Failed to execute 'insertAfter' on 'Node': parameter 1 is not of type 'Node'.")
  }
  
  if (targetNode !== null && !(targetNode instanceof Node)) {
    throw new TypeError("Failed to execute 'insertAfter' on 'Node': parameter 2 is not of type 'Node'.")
  }
  
  if (targetNode !== null && targetNode.parentNode !== this) {
    throw new DOMException("Failed to execute 'insertAfter' on 'Node': The node before which the new node is to be inserted is not a child of this node.")
  }

  if(!targetNode || this.lastChild == targetNode) {
    this.appendChild(newNode); 
  } else {
    this.insertBefore(newNode, targetNode.nextSibling);
  }
}

This way you can use some processing of parent.insertAfter(newElement, targetElment),這里模仿了 insertBefore.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template