HTMLFormControlsCollection: namedItem() 方法
HTMLFormControlsCollection.namedItem() 方法返回集合中name或id与指定名称匹配的 RadioNodeList 或 Element,如果没有节点匹配,则返回null。
请注意,此版本的 namedItem() 隐藏了从 HTMLCollection 继承的版本。与该版本一样,在 JavaScript 中,使用带有 String 的数组括号语法(例如 collection["value"])等同于 collection.namedItem("value")。
语法
js
namedItem(name)
[name]
参数
name-
一个字符串,将用于与此
HTMLFormControlsCollection对象中控件的name或id属性进行匹配。
返回值
- 一个
RadioNodeList、Element或null。
示例
使用 namedItem()
HTML
html
<form>
<label for="notes">Notes:</label>
<input id="notes" name="my-form-control" type="text" />
<label for="start">Start date:</label>
<input id="start" name="my-form-control" type="date" />
</form>
<div id="output"></div>
JavaScript
js
const form = document.querySelector("form");
const items = form.elements.namedItem("my-form-control");
const output = document.querySelector("#output");
const itemIDs = Array.from(items)
.map((item) => `"${item.id}"`)
.join(", ");
output.textContent = `My items: ${itemIDs}`;
结果
规范
| 规范 |
|---|
| HTML # dom-htmlformcontrolscollection-nameditem-dev |
浏览器兼容性
加载中…
另见
HTMLCollection.namedItem,它被替换了