日期:2014-05-16  浏览次数:20516 次

Js之appendChild() insertBefore() 和扩展的 insertAfter()
JavaScript之appendChild、insertBefore和insertAfter:
http://www.cnblogs.com/samlin/archive/2009/03/28/JavaScript-appendChild-insertBefore-insertAfter.html


使用prototype来扩展HTML节点元素的insertAfter()(注:ie下不适用,会报 HTMLElement(或Node)未定义):
http://snippets.dzone.com/posts/show/5179
// 一 扩展HTMLElement的prototype
HTMLElement.prototype.insertAfter = function(newNode, refNode) {
        if(refNode.nextSibling) {
            return this.insertBefore(newNode, refNode.nextSibling);
        } else {
            return this.appendChild(newNode);
        }
    }

// 二 扩展Node的prototype
Node.prototype.insertAfter = function(newNode, refNode) {
	if(refNode.nextSibling) {
		return this.insertBefore(newNode, refNode.nextSibling);
	} else {
		return this.appendChild(newNode);
	}
}