1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 添加到这里之前 <button onclick="myFunction()">xxx</button> <script> function myFunction() { let htm = `<span style="color: #ff6b81">在body标签后面插入内容</span>`;
let tempNode = document.createElement('div'); tempNode.id = 'insert-body' tempNode.className = 'insert-body' tempNode.innerHTML = htm tempNode.style.display = 'flex' tempNode.style.justifyContent = 'center'
let bodyDom = document.body; let theFirstChild = bodyDom.firstChild document.body.insertBefore(tempNode, theFirstChild)
if (document.getElementById('insert-body')) { console.log('添加成功'); }else{ console.log('添加失败'); } }
</script>
|