function insertAfter(newNode, referenceNode) {referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);}
您可以使用以下代码段对其进行测试:
function insertAfter(referenceNode, newNode) {referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);}
var el = document.createElement("span");el.innerHTML = "test";var div = document.getElementById("foo");insertAfter(div, el);
// create function, it expects 2 values.function insertAfter(newElement,targetElement) {// target is what you want it to go after. Look for this elements parent.var parent = targetElement.parentNode;
// if the parents lastchild is the targetElement...if (parent.lastChild == targetElement) {// add the newElement after the target element.parent.appendChild(newElement);} else {// else the target has siblings, insert the new element between the target and it's next sibling.parent.insertBefore(newElement, targetElement.nextSibling);}}
Node.prototype.insertAfter = function(newNode, referenceNode) {return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); // based on karim79's solution};
// getting required handlesvar refElem = document.getElementById("pTwo");var parent = refElem.parentNode;
// creating <p>paragraph three</p>var txt = document.createTextNode("paragraph three");var paragraph = document.createElement("p");paragraph.appendChild(txt);
// now we can call it the same way as insertBefore()parent.insertAfter(paragraph, refElem);
var raf, cb=function(){//create newnodevar link=document.createElement('link');link.rel='stylesheet';link.type='text/css';link.href='css/style.css';
//insert after the lastnodevar nodes=document.getElementsByTagName('link'); //existing nodesvar lastnode=document.getElementsByTagName('link')[nodes.length-1];lastnode.parentNode.insertBefore(link, lastnode.nextSibling);};
//check before inserttry {raf=requestAnimationFrame||mozRequestAnimationFrame||webkitRequestAnimationFrame||msRequestAnimationFrame;}catch(err){raf=false;}
if (raf)raf(cb); else window.addEventListener('load',cb);
Element.prototype.appendBefore = function (element) {element.parentNode.insertBefore(this, element);},false;
. appendAfter(元素)原型
Element.prototype.appendAfter = function (element) {element.parentNode.insertBefore(this, element.nextSibling);},false;
代码片段以查看所有操作:
/* Adds Element BEFORE NeighborElement */Element.prototype.appendBefore = function(element) {element.parentNode.insertBefore(this, element);}, false;
/* Adds Element AFTER NeighborElement */Element.prototype.appendAfter = function(element) {element.parentNode.insertBefore(this, element.nextSibling);}, false;
/* Typical Creation and Setup A New Orphaned Element Object */var NewElement = document.createElement('div');NewElement.innerHTML = 'New Element';NewElement.id = 'NewElement';
/* Add NewElement BEFORE -OR- AFTER Using the Aforementioned Prototypes */NewElement.appendAfter(document.getElementById('Neighbor2'));
div {text-align: center;}#Neighborhood {color: brown;}#NewElement {color: green;}
// source: https://github.com/jserz/domPlus/blob/master/src/insertAfter()/insertAfter.jsNode.prototype.insertAfter = Node.prototype.insertAfter || function (newNode, referenceNode) {function isNode(node) {return node instanceof Node;}
if(arguments.length < 2){throw(new TypeError("Failed to execute 'insertAfter' on 'Node': 2 arguments required, but only "+ arguments.length +" present."));}
if(isNode(newNode)){if(referenceNode === null || referenceNode === undefined){return this.insertBefore(newNode, referenceNode);}
if(isNode(referenceNode)){return this.insertBefore(newNode, referenceNode.nextSibling);}
throw(new TypeError("Failed to execute 'insertAfter' on 'Node': parameter 2 is not of type 'Node'."));}
throw(new TypeError("Failed to execute 'insertAfter' on 'Node': parameter 1 is not of type 'Node'."));};
// Parentconst el = document.body;// New Elementconst newEl = document.createElement("div");
// Insert Before Elementel.before(newEl);
// Insert After Element// Technically this would be invalid because// I already inserted newEl before el.// newEl changed location and is no longer a floating element.// You cant insert one element in two places at once.el.after(newEl);
// Another extra feature originally added with ChildNode is the .remove() method,// which deletes the element from the DOMel.remove();newEl.remove();
// Parentconst el = document.body;// New Elementconst newEl = document.createElement("div");
// Function You Needfunction insertAfter(el0, el1) {el0.parentNode.insertBefore(el1, el0.nextSibling);}
// Insert Before Elementel.insertBefore(newEl);
// Insert After ElementinsertAfter(el, newEl);
// Just remember you cant use insertAfter() or .insertBefore()// on newEl more than once.// You cant insert one element in two places at once.
// Parentconst el = document.body;// New Elementconst newEl = document.createElement("div");
// Custom MethodElement.prototype.insertAfter = function(new) {this.parentNode.insertBefore(new, this.nextSibling);}
// Insert Before Elementel.insertBefore(newEl)
// Insert After Elementel.insertAfter(newEl);
// Just remember you cant use .insertAfter() or .insertBefore()// on newEl more than once.// You cant insert one element in two places at once.
// You could create a simple nodevar node = document.createElement('p')
// And then get the node where you want to append the created node aftervar existingNode = document.getElementById('id_of_the_element')
// Finally you can append the created node to the exisitingNodeexistingNode.after(node)