【发布时间】:2017-06-22 00:19:32
【问题描述】:
我创建了一个类名为 div class= "postWindow" 的 div。
html:
<div class = "postWindow">
<div class = "userName">Initial Name</div>
<div class = "postTitle">Init title</div>
</div>
现在,我想要实现的是在一个 html 窗口中最多可以创建 10 个。 (最多因为帖子的数量可能在 1-10 之间变化,并且数量是动态的)
我尝试使用appendChild() 方法,但意识到它只填充了 div 作为新元素。它没有创建新的 postWindow
我的 .js 文件有以下功能:
function createPost(){
var count = 0;
while(count < upLimit){
$.ajax({
url: root + '/posts/'+curr,
type: "GET",
dataType: "JSON",
success: function(response){
postJson = response;
//im lost here
}
});
count++;
}
使用 console.log 我的 Json 响应是这样的
0: Object
title: "First post"
body: "This is a post"
id: 1
userId: 27
.
.
.
100: //same format of data as object 0
任何帮助将不胜感激! :D
【问题讨论】:
-
$(".postWindow").parent().append("<div class='postWindow'><div class='userName'>Initial Name</div><div class='postTitle'>Init title</div></div>"); -
您的 JSON
response看起来如何?将其添加到问题中会很有帮助。 -
那么这些 postWindows 的父容器是什么?数据是什么样的?
-
父容器就是看起来您的响应对象中有属性。像你猜的那样打电话给他们......
response.titleresponse.body等。
标签: javascript jquery html json ajax