解释:
API:
The getElementsByName() method returns a collection of all elements in the document with the specified name (the value of the name attribute), as an HTMLCollection object.
The HTMLCollection object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.
Tip: You can use the length property of the HTMLCollection object to determine the number of elements with the specified name, then you can loop through all elements and extract the info you want.
<!DOCTYPE html>
<html>
<body>
First Name: <input name="fname" type="text" value="Michael"><br>
First Name: <input name="fname" type="text" value="Doug">
<p>Click the button to get the tag name of the first element in the document that has a name attribute with the value "fname".</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementsByName("fname")[0].tagName;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>