我有如下用例,我在其中填充innerHTML
如下。现在的问题是我想在 forloop 的上下文中解析模板文字。有什么线索吗?
var blog_entries_dom = 'blog_entries';
var blog_entries = [
{
"title": "Hello World",
"id": 2,
"body": "<p>Hello World</p>"
},
{
"title": "World Hello",
"id": 3,
"body": "<p>World Hello</p>"
}
];
var blog_entry_template = `<div class="post-preview">
<a href="post.html">
<h2 class="post-title">
${item.title}
</h2>
<h3 class="post-subtitle">
${item.body}
</h3>
</a>
<p class="post-meta">Posted by <a href="#">Start Bootstrap</a> #Created At </p>
</div>
<hr>`;
var populate_children = function (element_id, objects_list, template) {
var element = document.getElementById(element_id);
var html = '';
for (var i = 0; i < list.length; i++) {
var item = list[i]
html += template;
}
element.innerHTML = html;
};
populate_children(blog_entries_dom, blog_entries, blog_entry_template);