How do you change video src using jQuery?

前端 未结 8 861
执笔经年
执笔经年 2020-12-12 14:08

How do you change the src of a HTML5 video tag using jQuery?

I got this HTML:

8条回答
  •  不知归路
    2020-12-12 14:45

    I've tried using the autoplay tag, and .load() .play() still need to be called at least in chrome (maybe its my settings).

    the simplest cross browser way to do this with jquery using your example would be

    var $video = $('#divVideo video'),
    videoSrc = $('source', $video).attr('src', videoFile);
    $video[0].load();
    $video[0].play();
    

    However the way I'd suggest you do it (for legibility and simplicity) is

    var video = $('#divVideo video')[0];
    video.src = videoFile;
    video.load();
    video.play();
    

    Further Reading http://msdn.microsoft.com/en-us/library/ie/hh924823(v=vs.85).aspx#ManagingPlaybackInJavascript

    Additional info: .load() only works if there is an html source element inside the video element (i.e. )

    The non jquery way would be:

    HTML

    JS

    var video = document.getElementById('videoID');
    video.src = videoFile;
    video.load();
    video.play();
    

提交回复
热议问题