Calling appendChild() on a null node does nothing.
时间: 2024-05-11 08:17:56 浏览: 86
This error occurs when you try to append a child element to a parent element that doesn't exist or is null.
To resolve this error, make sure that the parent element exists and is accessible in the current scope. You can also check if the parent element has been initialized or created before trying to append a child element to it.
Here's an example of how to create a new element and append it to an existing parent element:
```
// create a new div element
const newDiv = document.createElement('div');
// get the parent element by its ID
const parentElement = document.getElementById('parent');
// check if the parent element exists
if (parentElement) {
// append the new div element to the parent element
parentElement.appendChild(newDiv);
} else {
console.error('Parent element not found!');
}
```
In this example, we first create a new `div` element using the `createElement()` method. Then, we try to get the parent element by its ID using the `getElementById()` method. If the parent element exists, we append the new `div` element to it using the `appendChild()` method. If the parent element doesn't exist, we log an error message to the console.
阅读全文