用于.ogg循环播放的javascript/jquery启动按钮
javascript/jquery start button for .ogg looped play
下面的代码生成并循环一些.ogg文件的随机迭代次数。页面加载后,它们立即播放。
注释掉的部分是我尝试添加"开始"按钮。我在这里做错了什么,我如何用按钮而不是页面加载来启动它?
(还有:我怀疑像我所做的那样将php和javascript结合起来是一种糟糕的做法。我这样做是因为php中的随机化非常容易,而且我对js/jquery还是个新手。)
(编辑:见下面的编辑。我只是忘记关闭标签。)
<html>
<head>
<title>Audio Testing</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<?php
$randC = rand(2,4);
$C = $randC * 4;
$G = $C / 2;
echo "
<script type='"text/javascript'">
$(document).ready(function()
{
// $('"#start'").click(function()
// {
var time = 0;
var riff_time = 2920;
var base = 'riff';
for(i = 0; i < $C; i++)
{
var id = base + ((i + 1) );
window.setTimeout ('"document.getElementById(''" + id + '"').play()'", time);
time += riff_time;
}
var timeG = 0;
var riff_time = 2920;
var baseG = 'riffG';
for(iG = 0; iG < $G; iG++)
{
var idG = baseG + ((iG + 1) );
window.setTimeout ('"document.getElementById(''" + idG + '"').play()'", timeG);
timeG += riff_time;
// }
} // EDIT: This should be }); ... that fixes it.
});
</script>
"?>
<style type="text/css">
</style>
</head>
<body style="background-color: #999;">
<!--<button id="start">Start</button>
<br> -->
<?php
echo "C:<br>";
for ($i=1; $i<= $C; $i++) {
echo '<audio controls id="riff'.$i.'">
<source src="loopC.ogg" type="audio/ogg">
</audio>';
}
echo "<br>G:<br>";
for ($i=1; $i<= $G; $i++) {
echo '<audio controls id="riffG'.$i.'">
<source src="loopG.ogg" type="audio/ogg">
</audio>';
}
?>
</body>
</html>
您可以为开始按钮指定一个类start
以及和data-id
。
所以你的html启动按钮会像,
<img src="start-button.jpg" class="start" data-id="riff1">
//So this is start button for audio elementwith id riff1.
这将如何工作?
$('.start').click(function(){
$("#"+$(this).data('id')).play(); //This should do.
}
是的,在你的php文件中应该是这样的类型,
<!DOCTYPE html>
<html>
<head>
<title>Audio Testing</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<?php
$randC = rand(2,4);
$C = $randC * 4;
$G = $C / 2;
<script type="text/javascript">
$(document).ready(function()
{
// $("#start").click(function()
// {
var time = 0;
var riff_time = 2920;
var base = 'riff';
<?php for(i = 0; i < $C; i++)
{
echo " var id = base + ((i + 1) );
window.setTimeout ('"document.getElementById(''" + id + '"').play()'", time);
time += riff_time; ";
}
?>
var timeG = 0;
var riff_time = 2920;
var baseG = 'riffG';
<?php for(iG = 0; iG < $G; iG++)
{
echo " var idG = baseG + ((iG + 1) );
window.setTimeout ('"document.getElementById(''" + idG + '"').play()'", timeG);
timeG += riff_time; ";
// } ?>
}
});
</script>
<style type="text/css">
</style>
</head>
<body style="background-color: #999;">
<!--<button id="start">Start</button>
<br> -->
<?php
echo "C:<br>";
for ($i=1; $i<= $C; $i++) {
echo '<audio controls id="riff'.$i.'">
<source src="loopC.ogg" type="audio/ogg">
</audio>';
}
echo "<br>G:<br>";
for ($i=1; $i<= $G; $i++) {
echo '<audio controls id="riffG'.$i.'">
<source src="loopG.ogg" type="audio/ogg">
</audio>';
}
?>
</body>
</html>
相关文章:
- 我的jQuery插件参数没有正确启动,遇到了问题
- jQuery悬停在没有鼠标悬停的情况下启动
- jQuery mouseup在拖离链接后未启动
- 无线电输入更改的jQuery事件未启动
- Rails jquery ajax调用未启动
- Jquery时间启动计时器,我有一个计时器的代码,但它在页面加载时启动
- 重新启动jquery脚本后,角度停止工作
- jQuery事件侦听器多次启动
- 重新启动游戏jQuery功能不工作
- javascript资产中的jQuery未启动
- 使用Javascript或jQuery启动和停止引导转盘
- JQuery启动文件下载,然后运行一个函数
- Web启动JNLP Web从javascript或jquery启动
- 是否可以在jquery启动或每次操作时将变量传递给它
- 上一个完成后的jQuery启动函数
- 用于.ogg循环播放的javascript/jquery启动按钮
- 当文档准备就绪时,单击事件Jquery启动
- 如何在使用jQuery启动的模态中更新-data行属性
- 无法使用jQuery启动事件
- 使用悬停中的 jQuery 启动和停止计时器