getElementByTagName()
时间: 2024-01-25 08:39:12 浏览: 123
js getElementsByTagName的简写方式
`getElementsByTagName()` is a method in JavaScript that is used to select all the elements in an HTML document that have a specified tag name. It returns a collection of elements that match the specified tag name, which can be accessed using an index.
For example, the following code selects all the `<p>` elements in the document and changes their background color to red:
```
var paragraphs = document.getElementsByTagName("p");
for (var i = 0; i < paragraphs.length; i++) {
paragraphs[i].style.backgroundColor = "red";
}
```
This method is commonly used to manipulate or access elements in the DOM (Document Object Model) using JavaScript.
阅读全文