jQuery 从数组构建画廊
我有一个充满图像链接的数组,我想编写一个脚本,使用 javascript (带有 jQuery 库),将格式化 html 输出,如下所示:
<div id="gallery">
<div class="scrollable">
<div class="items">
<div>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
</div>
</div>
</div>
</div>
每处理 5 个图像链接后,它将添加另一个 div 容器在 items 类中。因此,如果数组包含 15 个图像链接,那么最终结果将类似于:
<div id="gallery">
<div class="scrollable">
<div class="items">
<div>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
</div>
<div>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
</div>
<div>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
</div>
</div>
</div>
</div>
依此类推,直到数组末尾。有什么想法吗?
编辑:对代码标识感到抱歉,发布时搞砸了。
编辑2:澄清。
I have an array filled with image links and I'd like to write a script that would, using javascript (with jQuery library), would format the html output like so:
<div id="gallery">
<div class="scrollable">
<div class="items">
<div>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
</div>
</div>
</div>
</div>
After every 5 image links it processes, it will add another div container inside the items class. So if the array contained 15 image links then the end result would be something like:
<div id="gallery">
<div class="scrollable">
<div class="items">
<div>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
</div>
<div>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
</div>
<div>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
<img src="URL_from_array"/>
</div>
</div>
</div>
</div>
and so on until the end of the array. Any ideas?
Edit: Sorry about the code identation, it screwed up when posting.
Edit2: Clarification.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
*编辑 - 我修复了代码,以便它可以动态创建更多图像,每组五个。我相信这就是你所问的一切。
这个怎么样?
尝试演示
*Edit - I fixed the code up so that it would dynamically create more images, in sets of five. I believe that's everything you asked.
How about this?
Try the demo