【发布时间】:2015-11-01 17:06:04
【问题描述】:
当我播放音频时,暂停音频,单击外部链接,然后返回音频所在的页面,我希望音频重置而不是离开我离开的地方,但它离开了我离开的地方离开了。
在我的 HTML5 页面中,我在结束 body 标记之前包含了这个链接: src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
HTML5:
<table id="multiplemp3">
<tr>
<th>
<audio controls>
<source src="http://geo-samples.beatport.com/lofi/5005876.LOFI.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</th>
</tr>
<tr>
<th>
<audio controls>
<source src="http://geo-samples.beatport.com/lofi/5005933.LOFI.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</th>
</tr>
<tr>
</tr>
</table>
<br>
<p><a href="https://en.wikipedia.org/wiki/Main_Page">More . . .</a></p>
<p><a href="https://en.wikipedia.org/wiki/Main_Page">Return to Home Page</a></p>
JS:
/* allows user to play one mp3 at a time */
/* declare variables: get tag from html */
var audios = document.getElementsByTagName("audio");
var links = document.getElementsByTagName("a");
/* bind() is used for binding audio attribute and parameters of a function call, audio is the current audio element played( comes from the binding above), we compare each element in audios with this, and pause all those which are not matching. */
for (var i=0; i<audios.length;i++) {
audios[i].onplay = pauseAllAudios.bind(null, audios[i]);
}
function pauseAllAudios(audio) {
for(var i=0; i<audios.length;i++)
if(audios[i]!=audio) {
audios[i].pause();
} else if (links.click() ) {
audios[i].stop();
}
};
【问题讨论】:
-
我试过在 Microsoft Edge 和 Firefox 上打开小提琴,当你返回时,音频都停止并设置为 0s。
-
当我在本地服务器上测试它时它不起作用。但由于我使用的是这个库:“ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js”也许我必须在服务器上测试它。谢谢你告诉我。
标签: javascript html audio