如何更改html5源标签并播放视频

时间:2013-07-08 10:11:44

标签: javascript html html5 html5-video

在我的视频模板网站中,我想在点击HQ时更改视频的源标记,只有视频应该开始播放,而不是点击播放按钮,链接的文本应该更改在同一个函数中nq

关注html代码

{{if hqnq == true }}     //jquery template variable no importance
   <div class="toolbar" id="controls">
      <div style="float:right">         
          <a href="#" data-role="button" data-theme="a" data-mini="true" data-inline="true" id="player_hq_nq">HQ</a>
          // the text of the link should change when you are clicking
      </div>
      <div style="clear:both"></div>    
  </div>
{{/if}}


<div id="video_player">
<video autobuffer controls autoplay>
    <source src="http://www.tools4movies.com/Droid/Default/Inception.mp4" type="video/mp4" width="auto" height="auto" id="video" />
</video>
</div>

至少应该有一个将执行任务的功能。但是怎么做呢?

<script type="text/javascript">
var button = document.getElementById('player_hq_nq');

function showVideo() {
    if (button.title == "nq") {
        document.getElementById("video").setAttribute("src", "http://www.tools4movies.com/Droid/Default/Inception.mp4");
        document.getElementById("video").load();
        document.getElementById("video").play();
    } else {
        document.getElementById("video").setAttribute("src", "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
        document.getElementById("video").load();
        document.getElementById("video").play();
        button.title = "nq";
    }
}
</script>

这是小提琴:http://jsfiddle.net/k68Zp/389/

1 个答案:

答案 0 :(得分:1)

您需要在HQ上调用 showVideo()。所以你需要在showVideo()函数上面添加以下代码。:

$(document).ready(function(){  
    $("#player_hq_nq").click(function(){  
        showVideo();  
   });  
});