开发者

HTML5 Video Javascript

开发者 https://www.devze.com 2023-02-01 09:07 出处:网络
I am not experienced in Javascript, I have the following script to play video files on Andriod phone, and it works fine.

I am not experienced in Javascript, I have the following script to play video files on Andriod phone, and it works fine.

 <scr开发者_如何转开发ipt type="text/javascript">
        function PlayMyVideo(arg) {
            var myVideo = document.getElementById([arg]);
            myVideo.play();
        }
    </script>
<video id="what" src="what.mp4" poster="" />
<input type="button" onclick="PlayMyVideo('what')" value="Play" />

I am trying to write the tag on the fly:

  <script type="text/javascript">
        function PlayVideo() {
            new_video = document.createElement('video');
            new_video.setAttribute('scr', 'what.mp4'); 
            new_video.play(); 
         }
    </script>
<input type="button" onclick="PlayVideo()" value="Play2" />

Nothing happen, would appreciate your suggestions. Thanks in advance


new_video.setAttribute('scr', 'what.mp4');

'scr' is misspelled. It should be 'src'.

and also you should wait for the movie to load before play


Well you're not appending the newly created tag to anything, so it can't play because it's in "memory"/"void", not on the screen.

<div id='plc'>&nbsp;</div>

<script type='text/javascript'>
function PlayVideo() {
new_video = document.createElement('video');
document.getElementById('plc').appendChild(new_video);
new_video.setAttribute('scr', 'what.mp4');
new_video.play();
}


you are creating the video element, you need to add it to the DOM before it will be visible, more info here: http://www.javascriptkit.com/javatutors/dom2.shtml

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号