2017-06-22 42 views
0

我有顯示的書,我有/沒有讀到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); 
    } 
+0

有什麼問題?你有什麼問題? – Carcigenicate

+1

您儘早關閉您的循環。你在第一個「if」之前做,它應該在「else」之後。 – Przemek

+1

你的循環沒有意義,你只是將'currentBook'(重新定義它btw)設置爲數組中的第二個索引X次。然後使用document.write覆蓋整個DOM,並使用可能未定義的變量。 –

回答

4

我覺得你想要做這樣的事情:

for (var i = 0; i < booksListArray.length; i++) { 
    var currentBook = booksListArray[i];  
    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); 
    } 
} 

有幾種錯誤/你的代碼的壞話:
- 您關閉循環太早
- 你的循環條件不使用數組的 - 你需要使用i訪問到您的currentBook
- 使用varlet初始化i變量
- 你應該填充的HTML element而是採用document.write

這是一個完整的工作示例:

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 
 
}]; 
 

 
var mydiv = document.getElementById('mylist'); 
 
for (var i = 0; i < booksListArray.length; i++) { 
 
    var currentBook = booksListArray[i];  
 
    if (currentBook.alreadyRead == true) { 
 
    \t mydiv.innerHTML += "I have already read " + currentBook.title + " by " + currentBook.author + "<br>"; 
 
    } else { 
 
    \t mydiv.innerHTML += "You still have to read " + currentBook.title + " by " + currentBook.author + "<br>"; 
 
    } 
 
}
<div id="mylist"></div>

+0

你是什麼意思,我太早關閉它了? –

+0

在引入條件之前('if' /'else'),您正在使用括號'}來關閉for循環。 – Marc

+0

好吧,我明白你的意思了,現在你會推薦我做什麼而不是文檔,寫入? –

2
for (var i = 0; i < booksListArray.length; i++) { 
    var currentBook = booksListArray[i]; //index should be i  
    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); 
    } 
}; 
+0

是的,我已經發現了,抱歉,但我仍然無法顯示它 –

相關問題