insertAfter实现2
2019-12-25 本文已影响0人
薛定谔的程序
<script>
window.onload = function () {
let newElement = document.createElement('strong');
let newText = document.createTextNode('strong');
newElement.appendChild(newText);
let oDiv = document.getElementById('div');
insertAfter(newElement,oDiv);
};
function insertAfter(newElement,targetElement) {
let parent = targetElement.parentNode;
if (targetElement == parent.lastChild){
parent.appendChild(newElement);
}else{
parent.insertBefore(newElement,targetElement.nextSibling);
}
}
</script>
</head>
<body>
<div id="div">This is a div!</div>
<p id="text">This is a text!</p>
<em>em em</em>
</body>
