手写js的insertAfter

function insertAfter(newElement, targetElement) {
        var parent = targetElement.parentNode;
        if(parent.lastChild == targetElement){
            parent.appendChild(newElement);
        }else{
            parent.insertBefore(newElement,targetElement.nextSibling);
        }
    }

还有这个下移的实现

function down(){
        var liCurrent = this.parentNode.parentNode;
        var next = liCurrent.nextSibling;
        while(next){
            if(next.nodeName == 'LI'){
                break;
            }
            next = next.nextSibling;
        }
        if(next) {
            insertAfter(liCurrent,next);
        } else {
            alert('已经是最后一个了');
        }
    }

 

posted @ 2018-11-01 14:45  菜の可怜  阅读(196)  评论(0编辑  收藏  举报