简体   繁体   English

循环遍历div以获取多个图像

[英]Looping over div for multiple images

My problem is I want to create many instances of the below div however with different image attributes. 我的问题是我想创建以下div的许多实例,但是它们具有不同的图像属性。 I want to increment the image name by 1 using a for loop. 我想使用for循环将图像名称加1。 For example: <div class="pm-gallery-post-item-container" style="background-image:url(img/gallery/2nd-white-coat1.jpg);"> I want to keep increment the number in the image name for n times. 例如: <div class="pm-gallery-post-item-container" style="background-image:url(img/gallery/2nd-white-coat1.jpg);">我想保持数字递增图像名称为n次。 So in the second iteration I want it to be <div class="pm-gallery-post-item-container" style="background-image:url(img/gallery/2nd-white-coat2.jpg);"> (notice how the file name ends with 2). 因此,在第二次迭代中,我希望它成为<div class="pm-gallery-post-item-container" style="background-image:url(img/gallery/2nd-white-coat2.jpg);"> (请注意文件名以2)结尾。

UPDATE: I want to use multiple images for the same kind of div. 更新: 我想对同一种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,我不想使用for循环来更改图像名称

Below is the div I want to do this to: 以下是我要执行的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>

You can use string concatenation to build the string for the image url. 您可以使用字符串串联来构建图像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
}

The above will work if you're creating the div elements one time. 如果您一次创建div元素,则以上内容将起作用。 If this needs to run more than once, you will need to track what number is next. 如果需要多次运行,则需要跟踪下一个数字。 There are several ways to do that. 有几种方法可以做到这一点。 Here's one way: 这是一种方法:

$(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);
  });
});

To replace the existing div with a new image 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);
    }
  });
});

Fiddle Demo of this: https://jsfiddle.net/zephyr_hex/ok6uv154/ 小提琴演示: https//jsfiddle.net/zephyr_hex/ok6uv154/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM