
DOMElement::insertAdjacentElement 함수를 사용할 때 부모 요소가 존재하지 않으면 에러가 발생합니다.
이러한 에러를 해결하기 위해 부모 요소가 존재하는지 확인하는 코드를 추가해야 합니다. 부모 요소가 존재하지 않으면 에러가 발생하지 않도록 처리할 수 있습니다.
예를 들어, 부모 요소가 존재하지 않으면 다음과 같이 처리할 수 있습니다.
#hostingforum.kr
php
$child = new DOMElement('p');
$parent = $child->parentNode;
if ($parent) {
$parent->insertAdjacentElement('beforebegin', $child);
} else {
// 부모 요소가 존재하지 않는 경우 처리
// 예를 들어, 부모 요소를 생성하고 자식 요소를 삽입할 수 있습니다.
$root = new DOMDocument();
$root->appendChild($child);
$parent = $root->documentElement;
$parent->insertAdjacentElement('beforebegin', $child);
}
또는, 부모 요소를 생성하고 자식 요소를 삽입할 수 있습니다.
#hostingforum.kr
php
$child = new DOMElement('p');
$parent = new DOMElement('div');
$parent->appendChild($child);
$parent->insertAdjacentElement('beforebegin', $child);
이러한 코드를 사용하면 부모 요소가 존재하지 않는 경우 처리할 수 있습니다.
2025-08-05 22:43