这是一个JSLinted,不引人注目的
Javascript示例,演示如何处理和使用结束的
mediaevent.在您的特定情况下,您将触发在您的结束事件处理程序中播放第二个音频文件.
单击播放列表中的项目开始播放.一个音频结束后,下一个音频开始.
标记:(注意,有意避免< li>元素之间的空格 – 这是为了简化使用nextSibling遍历DOM.)
- Space 1
- Space 2
- Space Lab
Stop
脚本:
// globals
var _player = document.getElementById("player"),_playlist = document.getElementById("playlist"),_stop = document.getElementById("stop");
// functions
function playlistItemClick(clickedElement) {
var selected = _playlist.querySelector(".selected");
if (selected) {
selected.classList.remove("selected");
}
clickedElement.classList.add("selected");
_player.src = clickedElement.getAttribute("data-ogg");
_player.play();
}
function playNext() {
var selected = _playlist.querySelector("li.selected");
if (selected && selected.nextSibling) {
playlistItemClick(selected.nextSibling);
}
}
// event listeners
_stop.addEventListener("click",function () {
_player.pause();
});
_player.addEventListener("ended",playNext);
_playlist.addEventListener("click",function (e) {
if (e.target && e.target.nodeName === "LI") {
playlistItemClick(e.target);
}
});