0
所以我一直叫數組「意見」顯示數組中的每個項目在HTML新行
comments: ["Comment1","Comment2","Comment3"]
現在我要顯示每個項目在HTML
其他的東西,一個新行從我的對象我像這樣顯示:
<p><%= singlegif.get("title") %></p>
但顯然把「意見」在上面的代碼顯示他們全部旁邊的海誓山盟.. 現在我該怎樣一個顯示/線路?
所以我一直叫數組「意見」顯示數組中的每個項目在HTML新行
comments: ["Comment1","Comment2","Comment3"]
現在我要顯示每個項目在HTML
其他的東西,一個新行從我的對象我像這樣顯示:
<p><%= singlegif.get("title") %></p>
但顯然把「意見」在上面的代碼顯示他們全部旁邊的海誓山盟.. 現在我該怎樣一個顯示/線路?
Javascript數組原型具有函數forEach(callback(element, index, array)),該函數對具有指定值的數組的每個元素執行一次提供的回調函數。對於已被刪除或已被初始化爲未定義的索引,不會調用它。
var commentsContainer = document.getElementById("demo");
comments: ["Comment1","Comment2","Comment3"];
comments.forEach(function(element, index){
// create paragraph for each comment
var elementForComment = document.createElement('p');
// set text of paragraph
elementForComment.innerText = element;
// add paragraph into comments holder.
commentsContainer.appendChild(elementForComment);
});
這看起來不像香草JavaScript給我。 –
也使用Backbone,JQuery和Underscore,抱歉! – vlovystack
也許這可以幫助你:http://stackoverflow.com/questions/9329446/for-each-in-an-array-how-to-do-that-in-javascript – Christine