Last active
December 15, 2016 06:17
-
-
Save tao4yu/9613934 to your computer and use it in GitHub Desktop.
Javascript DOM: insertAfter方法实现
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*JaavaScript DOM 只提供了insertBefore方法: | |
* parentElement.insertBefore(newElement, targetElement) | |
* targetElement.parentNode.insertBefore(newElement, targetElement) | |
* 而没有提供insertAfter方法,可以利用DOM已有的属性和方法将其实现,如下。 | |
*/ | |
function insertAfter(newElement, targrtElement) | |
{ | |
var parent = targetElement.parentNode; | |
if (parent.lastChild == targetElement) | |
{ | |
parent.appendChild(newElement); | |
} | |
else | |
{ | |
parent.insertBefore(newElement, targetElement.nextSibling); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你好,你实现的insertAfter方法,可以不用判断目标元素是否是最后一个元素吧?

参照javascript标准参考教程 4.4Node.insertBefore()