尝试使用构造函数通过单击按钮更新书籍的对象类。我不断收到错误,说无法找到3个变量(book1,book2和book3)。不确定为什么,但我相信它是一个非常简单的错误(或者可能是一个非常激烈的错误)。我希望信息显示在警报中,但是我可以使用document.getElementById()。innerHTML =;更新
?
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Book Object</title>
</head>
<body>
<h1>Two Examples of Books</h1>
<h3>Read Available Books</h3>
<input type = 'button' value = 'To Kill a Mockingbird' onclick = 'book1.read();'/>
<input type = 'button' value = 'The Outsiders' onclick = 'book2.read();'/>
<h3>Read in your own custom book</h3>
<input type = 'button' value = 'Enter your book' onclick = 'book3.promptNewBookInfo();'/>
<input type = 'button' value = 'Your Book' onclick = 'book3.read();'/>
<script type = "text/javascript">
var book1 = new Book("To Kill a Mockingbird", "Harper Lee", "David Johnson", 281, "Social Drama");
var book2 = new Book("The Outsiders", "S. E. Hinton", "(none)", 192, "Young Adult Fiction");
var book3 = new Book("tbd", "tbd", "tbd", 0, "tbd");
function Book(ti, au, ill, pgs, gnr) {
this.title = ti;
this.author = au;
this.illustrator = ill;
this.pages = pgs;
this.genre = gnr;
this.read = funcion() {
var newBook = this.title + " by" + this.author + "and illustrated by:" + this.illustrator + " is" + this.pages + " pages long" + "and is " + this.genre;
alert(newBook);
};
this.promptNewBookInfo = function()
{
this.title = prompt("What is the title?");
this.author = prompt("Who is the nook written by?");
this.illustrator = prompt("Who is the book illustrated by?");
this.pages = parseInt(prompt("How many pages is the book?"))
this.genre = prompt("What genre is your book?");
alert (this.title + "is now ready to be read!");
}
}
</script>
</body>
</html>
答案 0 :(得分:0)
拼写错误:funcion =&gt;功能
this.read = function() {
var newBook = this.title + " by" + this.author + "and illustrated by:" + this.illustrator + " is" + this.pages + " pages long" + "and is " + this.genre;
alert(newBook);
};
答案 1 :(得分:0)
它只是一个拼写错误
this.read = funcion() {
应该是
this.read = function() {
坚持下去!