2

所以我有一个带有一个 div 容器和一个按钮的简单页面,我希望用户能够点击按钮并将新视频加载到 div 中。

我总共有 8 个视频,页面从自动播放和循环播放的视频开始,然后如果用户点击页面底部的按钮,视频 2 被加载并自动播放和循环,直到他们再次点击该按钮加载视频 3.

希望这是有道理的!我敢肯定这很容易做到,但我知道这需要我几天的时间才能弄清楚!

任何帮助都会很棒!

这是我的代码,

<div id="vidBox">
    <video width= "977" height= "721" src= "1.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"</video>
</div>

$("#vidBox").empty().append( video width= "977" height= "721" src= "2.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay" );
$("#vidBox").empty().append( video width= "977" height= "721" src= "3.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay" );
$("#vidBox").empty().append( video width= "977" height= "721" src= "4.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay" );
etc etc etc

这是我的更新代码

<!DOCTYPE html> 
<html>
<head>
<title>Video Player</title> 
<script type="text/javascript">
    $("document").ready(function () {
    var vid1 = $("<video width= "977" height= "721" src= "1.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"</video>");
    var vid2 = $("<video width= "977" height= "721" src= "2.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"</video>");
    var vid3 = $("<video width= "977" height= "721" src= "3.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"</video>");
    var vid4 = $("<video width= "977" height= "721" src= "4.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"</video>");
    var vid5 = $("<video width= "977" height= "721" src= "5.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"</video>");
    var vid6 = $("<video width= "977" height= "721" src= "6.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"</video>");
    var vid7 = $("<video width= "977" height= "721" src= "7.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"</video>");
    var vid8 = $("<video width= "977" height= "721" src= "8.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"</video>");
});
</script>
<script>
    window.myVids = ["1.mp4", "2.mp4", "3.mp4", "4.mp4", "5.mp4", "6.mp4", "7.mp4", "8.mp4"];
</script>
<script>
   var currentIndex = 0;
   var $video = $("#vidBox video");
   $("#myButton").click(function() {
      video.attr("src", myVids[++currentIndex]);
   });
</script>
<link href="style.css" rel="stylesheet" type="text/css">
</head> 
<body>
<div id="vidBox">Content for New Div Tag Goes Here</div>
<button id="myButton">Click Me</button>
</body>
</html>
4

1 回答 1

0

这是您不希望它做的事情:)

有一个典型的方法,使用一些数据。不要在清空该区域然后附加某些内容的地方创建这行代码。如果您查看您的代码,您会发现,除了“src”属性外,其他所有内容都相同。那么为什么不让事情变得更简单、更可重用。

想想你让来自服务器的一些数据......把它们放到视图中

<script>
    window.myVids = ["1.mp4", "2.mpg", ...];
</script>

现在您在窗口中有一个简单的全球可用数据结构。虽然这不是最佳实践,但现在已经足够了。

现在连接你的按钮,它将替换 src 属性。我现在不确定,如果您只是更改 src 属性,它是否会播放下一部电影,但是您可以自己尝试。

<script>
   var currentIndex = 0;
   var $video = $("#vidBox video");
   $("#myButton").click(function() {
      video.attr("src", myVids[++currentIndex]);
   });
</script>

更新

哦,伙计,你的更新看起来很糟糕。好吧,你可以很幸运,我有一个美好的一天......

<!DOCTYPE html> 
<html>
<head>
<title>Video Player</title> 
<script type="text/javascript">
    $("document").ready(function () {
        window.myVids = ["1.mp4", "2.mp4", "3.mp4", "4.mp4", "5.mp4", "6.mp4", "7.mp4", "8.mp4"];
        var currentIndex = 1;
        var $video = $("#vidBox video");
        $("#myButton").click(function() {
             video.attr("src", myVids[++currentIndex]);
        });
    });
</script>
<link href="style.css" rel="stylesheet" type="text/css">
</head> 
<body>
<div id="vidBox">
    <video width= "977" height= "721" src= "1.mp4"  type= "video/mp4" loop= "loop" autoplay="autoplay"></video>
</div>
<button id="myButton">Click Me</button>
</body>
</html>
于 2013-06-30T22:21:54.833 回答