insertAfter实现2
作者:
薛定谔的程序 | 来源:发表于
2019-12-25 09:41 被阅读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>

image.png
本文标题:insertAfter实现2
本文链接:https://www.haomeiwen.com/subject/aggqoctx.html
网友评论