我有顯示的書,我有/沒有讀到DOM陣列煩有顯示我的代碼到DOM
var booksListArray = [
{
title: "Always Running",
author: "Luis J Rodriguez",
alreadyRead: true
},
{
title: "Hatchet",
author: "Gary Paulsen",
alreadyRead: true
},
{
title: "Autobiography of Malcolm X",
author: "Malcolm X",
alreadyRead: true
},
{
title: "Che Guevara: A Revolutionary Life",
author: "Jon Lee Anderson",
alreadyRead: false
},
{
title: "The Prince",
author: "Niccolo Machiavelli",
alreadyRead: false
}];
for (i = 0; i < booksListArray; i++) {
var currentBook = booksListArray[1];
};
if (currentBook.alreadyRead == true) {
document.write("I have already read " + currentBook.title + " by " + currentBook.author);
} else {
document.write("You still have to read " + currentBook.title + " by " + currentBook.author);
}
有什麼問題?你有什麼問題? – Carcigenicate
您儘早關閉您的循環。你在第一個「if」之前做,它應該在「else」之後。 – Przemek
你的循環沒有意義,你只是將'currentBook'(重新定義它btw)設置爲數組中的第二個索引X次。然後使用document.write覆蓋整個DOM,並使用可能未定義的變量。 –