js保存html对象,如何从js对象完全创建HTML?

只是一步一步地做。

var posts = [ //My object (array of posts)

{ //An example post

index: 1, //Identifies order of posts: 1 is oldest... >1 newest

id: "first", //An id for the post

date: { //Date will be listed next to name on post

month: 11,

day: 2,

year: 2018

},

name: "My Post", //Name of the post

text: "Text for the post...", //Actual Post

image: 'blogImage.png' //An image for the post

},

{ //An example post

index: 2, //Identifies order of posts: 1 is oldest... >1 newest

id: "first", //An id for the post

date: { //Date will be listed next to name on post

month: 11,

day: 2,

year: 2018

},

name: "My another Post", //Name of the post

text: "Text for another post...", //Actual Post

image: 'blogImage.png' //An image for the post

}

];

const container = document.getElementById('div-posts');

posts.forEach(function(post) {

let div = document.createElement('div');

div.className = 'card';

div.id = 'post_' + post.index; //or post.id provided itis unique

//create more elements instead of ".innerHTML" if you wish

div.innerHTML = '

' + post.name + '

' +

'

' + (new Date(post.date.year, post.date.month, post.date.day).toDateString()) + '
' +

'

'%20+%20post.image%20+%20'
' +

'

' + post.text + '

';

container.appendChild(div);

});

.card {

border: solid 1px #ccc;

width: 400px

}

.img img {

max-width: 100%

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值