不用jQuery来实现insertAfter()方法

// 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;

    // 如果after的这个元素是最后一个元素,调用方法 parent.appendChild(新元素)
    // if the parents lastchild is the targetElement...
    if (parent.lastChild == targetElement) {
        // add the newElement after the target element.
        parent.appendChild(newElement);
    } else {
        // 否则不是最后一个元素,调用方法 parent.insertBefore(新元素,元素的下一个兄弟)
        // else the target has siblings, insert the new element between the target and it's next sibling.
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}

results matching ""

    No results matching ""