我的网站上有几个按钮,它们是onclicks,js文件看起来像这样(例如一点点):
var bestMovie = function () {
document.getElementById("bestefilm").innerHTML = "The Fault In Our Stats | Budget: $12 miljoen - Opgeleverd: $65.3 miljoen";
};
var buttonClick7 = document.querySelector(".bestefilm");
buttonClick7.addEventListener("click", bestMovie);
我的html是这样的:
<input type="button" class="bestefilm" value="Welke film heeft het meest opgeleverd in juni 2014?" />
<p id="bestefilm"></p>
它工作正常,但我也添加了一个带对象的数组,我的js看起来像这样:
var top5Movies = function() {
var movies = new Array(5);
movies[0] = {title: "300: Rise Of An Empire", iMDBscore: "6.6", number: "#1"};
movies[1] = {title: "Street Fighter: Assassin's Fist", iMDBscore: "8.2", number: "#2"};
movies[2] = {title: "X-Men: Days of Future Past", iMDBscore: "8.5", number: "#3"};
movies[3] = {title: "The Grand Budapest Hotel", iMDBscore: "8.3", number: "#4"};
movies[4] = {title: "Non-stop", iMDBscore: "7.2", number: "#5"};
var geefAntwoord = movies[Math.round((movies.length - 1) * Math.random())];
var genereerRandom = document.getElementById("random1");
genereerRandom.innerHTML = geefAntwoord;
}
var buttonClick8 = document.querySelector(".random1")
buttonClick8.addEventListener("click", top5Movies);
和我的HTML一样:
<input type="button" class="random1" value="Willekeurige selectie van 5 films die het meest zijn gedownload in juni 2014" />
<p id="random1"></p>
事情是,当我按下按钮&#39; willekeurige selectie ..&#39;这意味着随机选择&#39;我得到了这个答案:[object Object]。
我该如何解决这个问题?感谢。
答案 0 :(得分:0)
替换
genereerRandom.innerHTML = geefAntwoord;
使用
genereerRandom.innerHTML = geefAntwoord.title;