代码示例:(标识:jsck_node_insertadjacenthtml_3)
<!DOCTYPE html>
<html>
<body>

<h1>Element 对象</h1>

<h2>insertAdjacentHTML() 方法</h2>

<button onclick="myFunction()">插入</button>

<p>单击“插入”可在标题前插入一个 span:</p>

<h2 id="myH2">我的标题</h2>

<script>
function myFunction() {
  const h2 = document.getElementById("myH2");
  let html = "<span style='color:red'>我的 span</span>";
  h2.insertAdjacentHTML("beforebegin", html);
}
</script>

</body>
</html>
运行结果: