[英]Object object print (Javascript)
I got a few buttons on my website, they are onclicks and the js file looks like this (a little part for example): 我在网站上有几个按钮,它们是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);
and my html is like this: 和我的HTML是这样的:
<input type="button" class="bestefilm" value="Welke film heeft het meest opgeleverd in juni 2014?" />
<p id="bestefilm"></p>
It works fine, but i also have added an array with objects, my js looks like this: 它工作正常,但是我还添加了一个带有对象的数组,我的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);
and my HTML like this: 和我的HTML是这样的:
<input type="button" class="random1" value="Willekeurige selectie van 5 films die het meest zijn gedownload in juni 2014" />
<p id="random1"></p>
The thing is, when i press the button 'willekeurige selectie..' which means 'a random selection' i get this answer: [object Object]. 问题是,当我按下按钮“ willekeurige selectie ..”(意思是“随机选择”)时,我得到以下答案:[object Object]。
How can i fix this? 我怎样才能解决这个问题? Thanks.
谢谢。
Replace 更换
genereerRandom.innerHTML = geefAntwoord;
With 用
genereerRandom.innerHTML = geefAntwoord.title;
And also see Object.prototype.toString
另请参阅
Object.prototype.toString
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.