Get the content of the p element with id="myElement":
const collection = document.getElementsByTagName("p");
const element = collection.namedItem("myElement");
let text = element.innerHTML;
Try it Yourself »
This shorthand produces the same result:
const collection = document.getElementsByTagName("p");
const element = collection["myElement"];
let text = element.innerHTML;
Try it Yourself »
The namedItem()
method returns a named element from an HTMLCollection.
The namedItem()
method uses the id or name attribute to identify the element.
An HTMLCollection is an array-like collection (list) of HTML elements.
The elements in a collection can be accessed by index (starts at 0).
The length Property returns the number of elements in the collection.
HTMLCollection.namedItem(
name)
HTMLCollection[name]
Parameter | Description |
name | Required. The value of the id attribute, or the name attribute, of the element. |
Type | Description |
Element | The element with the specified id or name.null if the element does not exist. |
HTMLCollection.namedItem()
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!