DOM的insertAfter函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DOM的insertAfter函数相关的知识,希望对你有一定的参考价值。
Vert useful function since there isn't an insertAfter function in the DOM. Call it inside scripts, it expectsinsertAfter(*the new element to be inserted*, *the element you want it to be inserted after*);
//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); } }
以上是关于DOM的insertAfter函数的主要内容,如果未能解决你的问题,请参考以下文章
DOM外部插入insertAfter()与insertBefore()
DOM—外部插入.after().before().insertAfter()和.insertBefore()与内部插入.prepend()和.prependTo()
jQuery基础(DOM篇,append(),after(),prepend(),insertAfter(),节点删除,遍历方法each())