What do [] brackets after a Jquery selector mean?

后端 未结 7 744
借酒劲吻你
借酒劲吻你 2021-01-18 07:22

I\'m using this code to play a preloaded mp3 file.

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

shuffle.play();

Shuffle is my id. I got the code o

7条回答
  •  南方客
    南方客 (楼主)
    2021-01-18 08:07

    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 []).

    Essentially the same as doing document.getElementById('shuffle')

    EDIT (as Matt pointed out)

    this will then allow you to do your .play() call to start your audio stream.

提交回复
热议问题