我有这样的div
<div id="news" class="col-sm-6 col-md-4 col-lg-4 pb-70">
<div class="post-prev-img">
<a href="blog-single-sidebar-right.html"><img src="images/blog/post-prev-1.jpg" alt="img"></a>
</div>
<div class="post-prev-title">
<h3><a href="blog-single-sidebar-right.html">TIME FOR MINIMALISM</a></h3>
</div>
<div class="post-prev-info">
JULE 10<span class="slash-divider">/</span><a href="http://themeforest.net/user/abcgomel/portfolio?ref=abcgomel">JOHN DOE</a>
</div>
<div class="post-prev-text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae, nostrum, cumque culpa provident aliquam commodi assumenda laudantium magnam illo nostrum.
</div>
<div class="post-prev-more-cont clearfix">
<div class="post-prev-more left">
<a href="blog-single-sidebar-right.html" class="blog-more">READ MORE</a>
</div>
<div class="right" >
<a href="blog-single-sidebar-right.html#comments" class="post-prev-count"><span aria-hidden="true" class="icon_comment_alt"></span><span class="icon-count">21</span></a>
<a href="http://themeforest.net/user/abcgomel/portfolio?ref=abcgomel" class="post-prev-count"><span aria-hidden="true" class="icon_heart_alt"></span><span class="icon-count">53</span></a>
<a href="#" class="post-prev-count dropdown-toggle" data-toggle="dropdown" aria-expanded="false" >
<span aria-hidden="true" class="social_share"></span>
</a>
<ul class="social-menu dropdown-menu dropdown-menu-right" role="menu">
<li><a href="#"><span aria-hidden="true" class="social_facebook"></span></a>
</li>
<li><a href="#"><span aria-hidden="true" class="social_twitter"></span></a></li>
<li><a href="#"><span aria-hidden="true" class="social_dribbble"></span></a></li>
</ul>
</div>
</div>
</div>
我需要这是我的&#34;骨架&#34;为我的其他divs。 现在我想创建一个for并在其中附加我的数组的内容并打印HTML页面中的每个div。我怎么做?
由于
答案 0 :(得分:1)
试试jQuery templates。您在 script 标签内编写HTML代码,在JS代码中,您可以调用它们并按照您的意愿使用它们:
<script id="bookTemplate" type="text/x-jQuery-tmpl">
<div>
<img src="BookPictures/${picture}" alt="" />
<h2>${title}</h2>
price: ${formatPrice(price)}
</div>
</script>
<强>用法:强>
$("#bookTemplate").tmpl(books).appendTo("#bookContainer");
答案 1 :(得分:1)
您可以按照以下方式执行此操作,
在你的html中制作一个模板并使用css #template隐藏,制作一个div来显示我们要动态制作的所有项目#list
<div id="list">
</div>
<div id="template">
<div class="col-sm-6 col-md-4 col-lg-4 pb-70">
<div class="post-prev-img">
<a href="blog-single-sidebar-right.html"><img src="{{img}}" alt="img"></a>
</div>
<div class="post-prev-title">
<h3><a href="blog-single-sidebar-right.html">{{name}}</a></h3>
</div>
<div class="post-prev-info">
JULE 10<span class="slash-divider">/</span><a href="http://themeforest.net/user/abcgomel/portfolio?ref=abcgomel">JOHN DOE</a>
</div>
<div class="post-prev-text">
{{info}}
</div>
<div class="post-prev-more-cont clearfix">
<div class="post-prev-more left">
<a href="blog-single-sidebar-right.html" class="blog-more">READ MORE</a>
</div>
<div class="right" >
<a href="blog-single-sidebar-right.html#comments" class="post-prev-count"><span aria-hidden="true" class="icon_comment_alt"></span><span class="icon-count">21</span></a>
<a href="http://themeforest.net/user/abcgomel/portfolio?ref=abcgomel" class="post-prev-count"><span aria-hidden="true" class="icon_heart_alt"></span><span class="icon-count">53</span></a>
<a href="#" class="post-prev-count dropdown-toggle" data-toggle="dropdown" aria-expanded="false" >
<span aria-hidden="true" class="social_share"></span>
</a>
<ul class="social-menu dropdown-menu dropdown-menu-right" role="menu">
<li><a href="#"><span aria-hidden="true" class="social_facebook"></span></a>
</li>
<li><a href="#"><span aria-hidden="true" class="social_twitter"></span></a></li>
<li><a href="#"><span aria-hidden="true" class="social_dribbble"></span></a></li>
</ul>
</div>
</div>
</div>
</div>
隐藏模板的CSS
#template {
display: none;
}
然后用循环创建所有需要的对象数据的数组并替换数据持有者。
然后将新的html插入dom
var assets= [
{
name: "Time for JS",
img: "http://via.placeholder.com/350x150",
link: "",
info:"Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae, nostrum, cumque culpa provident aliquam commodi assumenda laudantium magnam illo nostrum.",
},
{
name: "Time for CSS",
img: "http://via.placeholder.com/350x150",
link: "",
info:"ipsum dolor sit amet, consectetur adipisicing elit. Recusandae, nostrum, cumque culpa provident aliquam commodi assumenda laudantium magnam illo nostrum .consectetur adipisicing",
},
{
name: "Time for HTML",
img: "http://via.placeholder.com/350x150",
link: "",
info:"consectetur adipisicing consectetur adipisicing elit. Recusandae, nostrum, cumque culpa provident aliquam commodi assumenda laudantium magnam illo nostrum .consectetur adipisicing",
}
];
var html=""
assets.forEach(buildTemplate);
document.getElementById("list").innerHTML=html;
function buildTemplate(item) {
var template = document.getElementById("template").innerHTML;
template = template.replace("{{name}}", item.name);
template = template.replace("{{info}}", item.info);
template = template.replace("{{img}}", item.img);
html+=template;
}
这里有一个简单的没有jquery超快速硬编码JS。
https://jsfiddle.net/5yeh38LL/
使用js模板引擎有一个更简单的方法,谷歌有很多,但这说明了这是如何完成的以及模板系统的工作效率。