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 expects
insertAfter(*the new element to be inserted*, *the element you want it to be inserted after*);
  1. //create function, it expects 2 values.
  2. function insertAfter(newElement,targetElement) {
  3. //target is what you want it to go after. Look for this elements parent.
  4. var parent = targetElement.parentNode;
  5.  
  6. //if the parents lastchild is the targetElement...
  7. if(parent.lastchild == targetElement) {
  8. //add the newElement after the target element.
  9. parent.appendChild(newElement);
  10. } else {
  11. // else the target has siblings, insert the new element between the target and it's next sibling.
  12. parent.insertBefore(newElement, targetElement.nextSibling);
  13. }
  14. }

以上是关于DOM的insertAfter函数的主要内容,如果未能解决你的问题,请参考以下文章

DOM外部插入insertAfter()与insertBefore()

jQuery insertAfter 不是一个函数

dom方法insertAfter的实现

DOM—外部插入.after().before().insertAfter()和.insertBefore()与内部插入.prepend()和.prependTo()

jQuery基础(DOM篇,append(),after(),prepend(),insertAfter(),节点删除,遍历方法each())

5、dom 结构操作怎样添加、移除、移动、复制、创建和查找节点