大家好! 我真的需要你的帮助,因为我无法弄清楚为什么它不起作用。 我写的错误是:Uncaught TypeError:playlist1.toHTML不是函数
Uncaught TypeError: playlist1.toHTML is not a function
JS:
function test(){
var playlist1 = new Playlist();
var music1 = new Music('theurl');
var music2 = new Music('anotherUrl');
var container = document.getElementById('results');
container.innerHTML = playlist1.toHTML();
}
var id=0;
function Music(url){
this.id=id+1;
this.url=url;
this.name="unknown";
this.author="unknown";
this.album="unknown";
this.cover="unknown";
}
var nb=0;
function Playlist(){
nb=nb+1;
this.elements = [];
this.nom = 'playlist'+nb;
}
function toHTML(){
var text="";
for(var i=0; i<this.elements.length; i++){
text+= "<li>\n";
text+= "<div>\n";
text+= "<figure class='music' track='"+this.elements[i].getUrl()+"'></figure>";
text+= "<img src='"+this.elements[i].getCover()+"'>\n";
text+= "</div>\n";
text+= "<div>\n";
text+= "<h3>"+this.elements[i].getName()+"</H3>\n";
text+= "<h4>"+this.elements[i].getAuthor()+" - "+this.elements[i].getAlbum()+"</H4>\n";
text+= "</div>\n";
text+= "</li>\n";
}
return text;
}
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="arrays.js"></script>
</head>
<body>
<button onclick="test()">Make it</button>
<p id="results"></p>
</body>
</html>
我会真的请如果有人可以帮助我解决这个问题:)
您的播放列表定义中的this.toHTML = toHTML'也许? – IrkenInvader
你好,谢谢你试图帮助我,但我不认为我理解你的评论? - IrkenInvader – 0dium
'function Playlist(){ nb = nb + 1; this.elements = []; this.nom ='playlist'+ nb; this.toHTML = toHTML; }' – IrkenInvader