问题描述
我看到一些使用 document.getElementsByTagName(input)[0]
结尾处的[0]的代码,我没有任何想法,这是什么意思。我尝试在w3schools寻找它,并发现它们使用它,但没有解释为什么。
I see some code which makes use of a [0] at the end of document.getElementsByTagName(input)[0]
and I have no ideas what that does or what it means. I tried looking for it on w3schools and found them using it , but no explanations as to why.
请帮助我了解这个后缀意味着什么。这两个命令如何不同...
Would some kind soul please help me understand what this suffix implies. How do these two commands differ...
var y = document.getElementsByTagName("input")[0]
var x = document.getElementsByTagName("input")
推荐答案
嗯, getElementsByTagName
返回一个列表的元素,所以使用 [0]
访问该列表的第一个元素。
Well, getElementsByTagName
returns a list of elements, so with [0]
you are accessing the first element of that list.
这篇关于在document.getElementsByTagName(input)[0]的末尾添加[0]的效果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!