最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access video element inside div - Stack Overflow

programmeradmin1浏览0评论

I am new to javascript and i was wondering can I access the video element thats inside the div element?

<div id="player" class="player">

        <video id="element"></video>

</div>

I am new to javascript and i was wondering can I access the video element thats inside the div element?

<div id="player" class="player">

        <video id="element"></video>

</div>
Share Improve this question asked Aug 20, 2014 at 23:43 user3853858user3853858 2451 gold badge4 silver badges11 bronze badges 1
  • How do you access the div element? Can't you access the video element by id ? – Volune Commented Aug 20, 2014 at 23:46
Add a ment  | 

4 Answers 4

Reset to default 1

var el = document.getElementById("element");

If you were using jQuery you could target it like this:

var el = $('video#element');

In pure javascript?

for that you'd use something like var video = document.getElementById('element');

here's a fiddle

I added a console.log statement so you can see the element showing up in the developer console.

Since the element has an id, you can simply get it directly :

var myVideo = document.getElementById("element");

If it didn't have an id for some reasons, you could do that :

var myVideo = document.querySelector("#player video");

That's very basic JS so you can find help on that anywhere. And specifically on querySelector here, for example.

You can just use regular javaScript to access the video element, but using it's id and the getElementById function.

var video = document.getElementById('element');
// Play Video
video.play();

If you're using jQuery, you can also do this:

var $video = $("#element");
$video.get(0).play();
发布评论

评论列表(0)

  1. 暂无评论