HTML DOM item() 方法从节点列表对象返回一个节点。
注意− item() 方法用于在节点列表对象中提供索引值。它与通过在节点列表对象的“[index]”中传递索引值来获取节点相同。
用法
以下是语法 -
调用 item(index),其中 index 从 0 开始
nodeListObject.item(index)
示例
让我们看一个例子item()方法 -
<!DOCTYPE html>
<html>
<head>
<title>item()</title>
<style>
form {
width:70%;
margin:0 auto;
text-align:center;
}
* {
padding:2px;
margin:5px;
}
input[type="button"] {
border-radius:10px;
}
ul{
width:30%;
margin:0 auto;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>item( )</legend>
<h2>Menu</h2>
<ul>
<li onclick="getOrder(0)">Mac & Cheese</li>
<li onclick="getOrder(1)">Pizza</li>
<li onclick="getOrder(2)">Burger</li>
</ul>
<div id="divDisplay">Click on menu item to order it</div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var menuSelect = document.getElementsByTagName("li");
function getOrder(index) {
divDisplay.textContent = 'Order:'+(menuSelect.item(index)).textContent;
}
</script>
</body>
</html>
输出
这将产生以下输出 -
点击前‘<li>’元素 -
点击后‘<li>’元素 -
相关用法
- HTML DOM iFrame用法及代码示例
- HTML DOM isSameNode()用法及代码示例
- HTML DOM insertAdjacentElement()用法及代码示例
- HTML DOM indexedDB deleteDatabase()用法及代码示例
- HTML DOM indexedDB databases()用法及代码示例
- HTML DOM insertAdjacentHTML()用法及代码示例
- HTML DOM insertAdjacentText()用法及代码示例
- HTML DOM isDefaultNamespace()用法及代码示例
- HTML DOM indexedDB cmp()用法及代码示例
- HTML DOM isEqualNode()用法及代码示例
- HTML DOM ins cite属性用法及代码示例
- HTML DOM ins dateTime属性用法及代码示例
- HTML DOM insertBefore()用法及代码示例
- HTML DOM indexedDB open()用法及代码示例
- HTML DOM Style overflowY属性用法及代码示例
- HTML DOM Document hidden属性用法及代码示例
- HTML DOM IFrame用法及代码示例
- HTML DOM Textarea cols属性用法及代码示例
- HTML DOM Style pageBreakAfter属性用法及代码示例
- HTML DOM Base href属性用法及代码示例
注:本文由纯净天空筛选整理自AmitDiwan大神的英文原创作品 HTML DOM item() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。