The insertBefore() method is a DOM manipulation method that allows you to insert a new element before an existing element in the DOM. The syntax for the insertBefore() method is as follows:

parentElement.insertBefore(newElement, existingElement);

Here, parentElement is the element that contains the existing element, newElement is the element that you want to insert, and existingElement is the element that you want to insert the new element before.

For example, if you have the following HTML:

<div id="parent">
  <p id="existing">Existing element</p>
</div>

And you want to insert a new paragraph element before the existing element, you can use the insertBefore() method as follows:

var parent = document.getElementById("parent");
var existing = document.getElementById("existing");
var newElement = document.createElement("p");
newElement.innerHTML = "New element";
parent.insertBefore(newElement, existing);

This will result in the following HTML:

<div id="parent">
  <p>New element</p>
  <p id="existing">Existing element</p>
</div>
``
insertBefore

原文地址: https://www.cveoy.top/t/topic/eHox 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录