【发布时间】:2025-12-31 09:40:11
【问题描述】:
我的问题是我想创建以下 div 的许多实例,但是具有不同的图像属性。我想使用 for 循环将图像名称增加 1。例如:<div class="pm-gallery-post-item-container" style="background-image:url(img/gallery/2nd-white-coat1.jpg);"> 我想将图像名称中的数字保持增加 n 次。所以在第二次迭代中,我希望它是 <div class="pm-gallery-post-item-container" style="background-image:url(img/gallery/2nd-white-coat2.jpg);">(注意文件名如何以 2 结尾)。
下面是我要这样做的 div:
<div class="isotope-item size3 col-lg-4 col-md-4 col-sm-12 col-xs-12 white-coat-talent all">
<div class="pm-gallery-post-item-container" style="background-image:url(img/gallery/2nd-white-coat1.jpg);">
<div class="pm-gallery-post-item-info-container">
<div class="pm-gallery-item-excerpt">
<p>LPSA hosted a talent show “White Coat Talent”, showcasing the talents of pharmacy students from all over Lebanon at Saint Joseph University.</p>
<ul class="pm-gallery-item-btns">
<li><a class="fa fa-camera lightbox" data-rel="prettyPhoto[white-coat-talent]" href="img/gallery/white-coat-talent11.jpg"></a></li>
<li><a class="fa-bars" href="#"></a></li>
</ul>
</div>
</div>
<a class="pm-gallery-item-expander fa fa-plus" href="#"></a>
</div>
<div class="pm-gallery-item-title">
<p>White Coat Talent</p>
</div>
</div>
【问题讨论】:
-
您是否正在寻找一个 JavaScript 函数来在 DOM 中创建这些
divs? -
我更新了我的描述,也许它会帮助传达我的问题。
-
@WaelAboulHosn :您是在问如何在循环中替换/更改图像 url?
-
是的,我想使用循环更改图像 url,因为图像的编号命名不同。
标签: javascript html image loops