# DOMElement::insertAdjacentElement

(PHP 8 >= 8.3.0)

DOMElement::insertAdjacentElement — Insert adjacent element

### 说明

public DOMElement::insertAdjacentElement(string `$where`, DOMElement `$element`): ?DOMElement

Inserts an element at a relative position given by `where`.

### 参数

`where`

  - `beforebegin` - Insert before the target element.
  - `afterbegin` - Insert as the first child of the target element.
  - `beforeend` - Insert as the last child of the target element.
  - `afterend` - Insert after the target element.
`element`

  The element to insert.

### 返回值

Return DOMElement or `null` on failure.

### 示例

示例 #1 DOMElement::insertAdjacentElement() example

`<?php
$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><container><p>foo</p></container>');
$container = $dom->documentElement;
$p = $container->firstElementChild;
$p->insertAdjacentElement('beforebegin', $dom->createElement('A'));
echo $dom->saveXML();
?>`

以上示例会输出：

    <?xml version="1.0"?>
    <container><A/><p>foo</p></container>

### 参见

- DOMElement::insertAdjacentText() - Insert adjacent text

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open