TypeError: document.querySelectorAll(...).getAttribute is not a function
时间: 2024-05-13 13:13:39 浏览: 13
This error occurs when you try to call the `getAttribute()` method on a NodeList returned by `querySelectorAll()` method. The `querySelectorAll()` method returns a collection of nodes that match a specified CSS selector, not a single element with an attribute.
To fix this error, you need to iterate over the NodeList using a loop and call the `getAttribute()` method on each element in the collection.
Here's an example:
```
const elements = document.querySelectorAll('.my-class');
elements.forEach(element => {
const attributeValue = element.getAttribute('data-my-attribute');
console.log(attributeValue);
});
```
In this example, we are selecting all elements with a class of `my-class`, iterating over the collection using the `forEach()` method, and calling the `getAttribute()` method on each element to retrieve the value of the `data-my-attribute` attribute.