Skip to content

Latest commit

 

History

History
26 lines (17 loc) · 775 Bytes

insertBefore.md

File metadata and controls

26 lines (17 loc) · 775 Bytes
标题 标签
insertBefore(前面插入元素) browser(浏览器)

在指定元素的开始之前插入一个 HTML 字符串。

  • 使用位置为 'beforebegin' 的 Element.insertAdjacentHTML() 来解析 htmlString 并将其插入到 el 的开始之前。
const insertBefore = (el, html) => el.insertAdjacentHTML('beforebegin', html);

调用方式:

insertBefore(document.getElementById('myId'), '<p>before</p>');
// <p>before</p> <div id="myId">...</div>

应用场景

结果如下:

<iframe src="codes/javascript/html/insertBefore.html"></iframe>