admin管理员组文章数量:1405983
So i have been doing some JavaScript and is currently learning jQuery. Time to time i get a bit confused by how jQuery does things pared to JS.
Got an HTML audio tag that looks something like this:
<audio id="audio" src="audio.mp3"></audio>
With JS i simply play it with:
document.getElementById('audio').play();
So i figured, doing the same with jQuery would just be:
$('#audio').play();
That doesn't work, instead i have to write it like this:
$('#audio')[0].play();
Can anyone explain this to me?
Thanks.
So i have been doing some JavaScript and is currently learning jQuery. Time to time i get a bit confused by how jQuery does things pared to JS.
Got an HTML audio tag that looks something like this:
<audio id="audio" src="audio.mp3"></audio>
With JS i simply play it with:
document.getElementById('audio').play();
So i figured, doing the same with jQuery would just be:
$('#audio').play();
That doesn't work, instead i have to write it like this:
$('#audio')[0].play();
Can anyone explain this to me?
Thanks.
Share Improve this question asked Jul 22, 2015 at 13:21 qua1ityqua1ity 6232 gold badges9 silver badges28 bronze badges1 Answer
Reset to default 4The following code :
$('#audio').play();
gives you back an array of elements that match the query selector wrapped with jQuery functions, this is how jQuery works, in your case it will give an array with one element .
the .play function is not a jQuery function and works directly on the html element, thus, you need to go to the first element in the jQuery array and then use the .play function
本文标签: javascriptjQuery play AudioStack Overflow
版权声明:本文标题:javascript - jQuery play Audio - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744967752a2635053.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论