addEventListener不';不能用javascript响应

addEventListener不';不能用javascript响应,javascript,addeventlistener,Javascript,Addeventlistener,addEventListener在“var播放按钮”部分工作不正常。我做错了什么 在控制台中运行playlist.prototype.play功能时。该代码可以运行,但我似乎无法将其绑定到HTML按钮 function mediaPlayer(title, artist){ this.title = title; this.artist = artist; } var songOne = new Audio('media/song_1.mp3'); var songTwo =

addEventListener在“var播放按钮”部分工作不正常。我做错了什么

在控制台中运行playlist.prototype.play功能时。该代码可以运行,但我似乎无法将其绑定到HTML按钮

function mediaPlayer(title, artist){
    this.title = title;
    this.artist = artist;
}

var songOne = new Audio('media/song_1.mp3');
var songTwo = new Audio('media/song_2.mp3');
var songThree = new Audio('media/song_3.mp3');
var songFour = new Audio('media/song_4.mp3');
var songFive = new Audio('media/song_5.mp3');

mediaPlayer.prototype = Object.create(playlist.prototype);

function playlist(){
    this.mediaList = [songOne, songTwo, songThree, songFour, songFive];
    this.nowPlayingIndex = 0;
}

function viewPlaylist(){
    playlist.call(this);
    for(var i = 0; i < mediaList.length; i++){
        console.log(mediaList[i]);
    }
}


playlist.prototype.play = function(){
    var currentMedia = this.mediaList[this.nowPlayingIndex];
    console.log(currentMedia);
    currentMedia.play();

}

var playButton = document.getElementById("playButton");
    playButton.addEventListener('click', function(){
    currentMedia.play();
    document.getElementById("nowPlayingSpan").innerHTML = currentMedia;
    document.getElementById("upNextSpan").innerHTML = currentMedia[1];
}) ;

playlist.prototype.pause = function(){
    var currentMedia = this.mediaList[this.nowPlayingIndex];
    currentMedia.pause();
}

var pauseButton = document.getElementById("pauseButton");
    pauseButton.addEventListener('click', function(){
    currentMedia.pause();   
}) ;
功能mediaPlayer(标题、艺术家){
this.title=标题;
这个艺术家=艺术家;
}
var songOne=新音频(“媒体/歌曲1.mp3”);
var songTwo=新音频('media/song_2.mp3');
var songThree=新音频('media/song_3.mp3');
var songFour=新音频(“媒体/歌曲4.mp3”);
var songFive=新音频(“媒体/song_5.mp3”);
mediaPlayer.prototype=Object.create(playlist.prototype);
函数播放列表(){
this.mediaList=[songOne,songTwo,songThree,songFour,songFive];
此.nowPlayingIndex=0;
}
函数viewPlaylist(){
播放列表。呼叫(此);
对于(var i=0;i
因为
cuurrentMedia
未在
addEventListener
范围内声明,所以您应该将
cuurrentMedia
替换为
playlist.mediaList[playlist.nowPlayingIndex]
您在函数内定义了当前媒体,并且它只属于该函数(当前媒体的范围就是函数)

}

将其单独定义为全局变量,并在函数内赋值

var currentMedia = {};
playlist.prototype.play = function(){
    currentMedia = this.mediaList[this.nowPlayingIndex];
    console.log(currentMedia);
    currentMedia.play();
}

因为未定义
currentMedia
,谢谢您的回复。我现在得到未捕获的TypeError:currentMedia.play不是HTMLLIElement上的函数。请尝试currentMedia.get(0.play();
var currentMedia = {};
playlist.prototype.play = function(){
    currentMedia = this.mediaList[this.nowPlayingIndex];
    console.log(currentMedia);
    currentMedia.play();
}