【问题标题】:Looping over div for multiple images循环遍历多个图像的 div
【发布时间】: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 结尾)。

更新: I want to use multiple images for the same kind of div. So instead of creating many divs that are the same but differ with image I want to use a for loop to change the image name

下面是我要这样做的 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


【解决方案1】:

您可以使用字符串连接来构建图像 url 的字符串。

var loops = 3;
for (var i = 2; i < (loops + 2); i++) {
  var newElem = $('<div/>').addClass('pm-gallery-post-item-container').css('background-image', 'url(img/gallery/2nd-white-coat' + i + '.jpg)');
  //do something with newElem, such as append to the DOM
}

如果您一次创建 div 元素,上述方法将起作用。如果这需要运行多次,您将需要跟踪下一个数字。有几种方法可以做到这一点。这是一种方法:

$(document).ready(function() {
  var iteration = 2;
  $('#myBtn').on('click', function() {
    var loops = 3;
    for (var i = iteration; i < (loops + iteration); i++) {
      var newElem = $('<div/>').addClass('pm-gallery-post-item-container').css('background-image', 'url(img/gallery/2nd-white-coat' + i + '.jpg)');
    }
    iteration += loops;
    console.log(iteration);
  });
});

用新的图片 url 替换现有的 div:

$(document).ready(function() {
  $('#myBtn').on('click', function() {
    var existingLi = $('.pm-gallery-post-item-container[style*="background-image:url"]');
    var style = existingLi.attr('style').split('-');
    var number = style[3].match(/\d+/);
    if (number !== null) {
      style[3] = style[3].replace(number, parseInt(number) + 1);
      var newStyle = style.join('-');
      $(existingLi).attr('style', newStyle);
    }
  });
});

这个的小提琴演示:https://jsfiddle.net/zephyr_hex/ok6uv154/

【讨论】:

  • 我尝试了您提供的第一个解决方案,但没有成功。我更新了描述,也许它会帮助你更好地理解我的问题。
  • @WaelAboulHosn - 我用代码更新了我的答案,显示了如何替换 div 上的现有样式。从您的问题中不清楚循环在哪里发挥作用,因此您需要将您拥有的任何循环机制与我关于如何替换样式的建议相结合。
最近更新 更多