在document.getElementsByTagName(input)[0]的末尾添加[0]的效果是什么? [英] What is the effect of adding [0] at the end of document.getElementsByTagName(input)[0]

查看:556
本文介绍了在document.getElementsByTagName(input)[0]的末尾添加[0]的效果是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些使用 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]的效果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆