问题描述
这是我不明白的语法是这样的:
The syntax that I don't understand is this:
$("#profilePhotoFileUpload")[0]
我经常被看到这个语法pretty,我已经忽略了一段时间,因为我从来没有使用它。但是现在,为了从这篇文章了解code How我上传使用Parse.com javascriptSDK图像?,我再也不能忽略它。
I have been seeing this syntax pretty frequently and I've ignored it for a while because I never had to use it. But now, in order to understand the code from this post How do I upload an image using the Parse.com javascriptSDK?, I cannot ignore it any longer.
我知道 [0]
此语法用于引用数组一般。但它似乎有点怪异,要一个 ID
引用会产生某种类型的数组。
I know that [0]
this syntax is used to refer to an array usually. But it seems a little weird that a reference to an id
would generate an array of some sort.
推荐答案
通过追加 [0]
来jQuery对象将返回第一 DOM元素。
By appending [0]
to the jQuery object will return the first DOM element.
当你在这里使用ID选择,会有这么使用 [0]
有道理数组中只有一个元素。如果您选择多个元素,您还可以使用任意数量是0和元素的数量之间就可以得到相应的DOM元素。
As you're using id selector here, there will be only one element in the array so using [0]
makes sense. If you are selecting multiple elements you can also use any number which is between 0 and number of elements you can get corresponding DOM element.
jQuery的文档
一个jQuery对象包含了文档对象模型(DOM)已经从HTML字符串创建或从文档中选择元素的集合。由于jQuery方法经常使用CSS选择器来匹配从文档元素,设置在一个jQuery对象元素通常被称为一组匹配的元素或选定元素的。
jQuery对象本身的行为很像一个数组;它有一个长度
属性,并在对象中的元素可以通过其数字索引来访问 [0]
到 [长度1]
。需要注意的是一个jQuery对象实际上不是一个JavaScript数组对象,因此它不具有真正的数组对象的所有方法,如加入()
。
The jQuery object itself behaves much like an array; it has a length
property and the elements in the object can be accessed by their numeric indices [0]
to [length-1]
. Note that a jQuery object is not actually a Javascript Array object, so it does not have all the methods of a true Array object such as join()
.
这篇关于什么是$(选择)[0] jQuery中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!