简体   繁体   English

Jquery 选择器后面的 [] 括号是什么意思?

[英]What do [] brackets after a Jquery selector mean?

I'm using this code to play a preloaded mp3 file.我正在使用此代码播放预加载的 mp3 文件。

var shuffle = $("#shuffle")[0]; 

shuffle.play();

Shuffle is my id. Shuffle是我的id。 I got the code off the net but I CANNOT figure out what the [0] after the jquery selector does.我从网上得到了代码,但我无法弄清楚 jquery 选择器之后的 [0] 是什么。 The sound does not play if I remove it.What does it do?如果我删除它,声音不会播放。它有什么作用?

thanks谢谢

jQuery is an array-like object that contains all of your matched elements. jQuery 是一个类似数组的对象,它包含所有匹配的元素。 Often times, jQuery will by default apply its changes to the first element in the collection:通常情况下,jQuery 会默认将其更改应用于集合中的第一个元素:

$("li").css("display"); // display val of first element, not all elements.

Even though many li elements could have been found, the jQuery object tells us about the first implicitly.尽管可以找到许多li元素,但 jQuery 对象隐式地告诉我们第一个元素。 We could explicitly instruct it to do so by using the $.get method:我们可以使用$.get方法明确指示它这样做:

$("li").get(0); // Returns first DOM element
$("li")[0]; // Also returns first DOM element

We could check the nodeName to verify this:我们可以检查nodeName来验证这一点:

$("li").get(0).nodeName; // LI
$("li")[0].nodeName; // LI

If we look under the covers, we can see how $.get() is implemented:如果我们深入了解,我们可以看到$.get()是如何实现的:

get: function(num) {
  return num == null 
    ? this.toArray() 
    : ( num < 0 
          ? this[ this.length + num ] 
          : this[ num ] );
}

From this we can see that when no argument is provided, the entire collection of element is converted to an array, and then returned.由此我们可以看出,当没有提供参数时,整个元素集合被转换为一个数组,然后返回。 When an argument is provided, for instance 2 , we return the element as index 2. If -2 is provided, this is added to the length (suppose the length is 5, 5+(-2) is 3) and the resulting number is used as the index.当提供参数时,例如2 ,我们将元素作为索引 2 返回。如果提供-2 ,则将其添加到长度(假设长度为 5,5+(-2) 为 3)和结果数字用作索引。

So with regards to your particular example:因此,关于您的特定示例:

var shuffle = $("#shuffle")[0];
shuffle.play();

jQuery is used to get any element that has the id value of shuffle . jQuery 用于获取 id 值为shuffle任何元素。 This returns the jQuery array-like object.这将返回类似 jQuery 数组的对象。 But your play() method doesn't exist on the jQuery object, it exists on the #shuffle object.但是您的play()方法不存在于 jQuery 对象中,它存在于#shuffle对象中。 As such, you need to get the first element in the collection.因此,您需要获取集合中的第一个元素。

You could use $.get(0) , however as we just saw, this would just be adding one more step.你可以使用$.get(0) ,但是正如我们刚刚看到的,这只是增加了一个步骤。 Internally, jQuery would perform the same code you're performing above, [0] .在内部,jQuery 将执行您在上面执行的相同代码[0]

In the direct context of your question, $("#shuffle") is a selector of an id, which returns a jQuery object (not an array per-se, but it has an array-like structure), then the [0] part is actually returning a native DOMElement object of the element with id shuffle instead of the jQuery object returned by calling $('#shuffle') (without the [] ).在您的问题的直接上下文中, $("#shuffle")是一个 id 的选择器,它返回一个jQuery对象(本身不是数组,但它具有类似数组的结构),然后[0]部分实际上是返回带有 id shuffle的元素的本机DOMElement对象,而不是通过调用$('#shuffle') (没有[] )返回的jQuery对象。

Essentially the same as doing document.getElementById('shuffle')本质上和做document.getElementById('shuffle')

EDIT (as Matt pointed out)编辑(正如马特指出的那样)

this will then allow you to do your .play() call to start your audio stream.这将允许您执行.play()调用以启动音频流。

$("#shuffle") will return an array of elements, according with your query, like [div,span,etc] $("#shuffle")将返回一个元素数组,根据您的查询,如[div,span,etc]
$("#shuffle")[n] means that you are selecting the nth element of this array $("#shuffle")[n]表示您正在选择此数组的第n个元素
In this case, $("#shuffle")[0] selects the first element of this array 在这种情况下, $("#shuffle")[0]选择此数组的第一个元素

The brackets after the $('#shuffle') get the first element of that selector provided. $('#shuffle') 后面的括号获取提供的该选择器的第一个元素。

$('div.test')[0];

<div class="test"></div> <-- this one would get returned
<div class="test"></div>
<div class="test"></div>

它返回包含匹配元素集中第一个元素的本机 javascript 对象。

The nth element of the returned array.返回数组的第 n 个元素。 The same as in regular javascript or php and a good part of the programming languages which support arrays.与常规 javascript 或 php 以及支持数组的编程语言的很大一部分相同。

JQuery returns an array. JQuery 返回一个数组。 [0] takes the first item in the array. [0] 取数组中的第一项。

它意味着要处理的对象的时间顺序总是在数组 [0],[1],[2] 上使用...你可以检查here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM