简体   繁体   English

jQuery在div中克隆活动的img,并将其作为div的背景图片

[英]jQuery clone the active img inside a div and make it the div's background image

This is the html that I have so far: 这是我到目前为止拥有的html:

<div class="testimonials">
  <div class="item active">
    <div class="testimonial-bg"></div>
    <div class="testimonial-img">
        <img src="img/testimonial-img1.jpg" alt="" />
        <p>
            Hanna's Story
        </p>
    </div>
  </div>
  <div class="item">
    <div class="testimonial-bg"></div>
    <div class="testimonial-img">
        <img src="img/testimonial-img2.jpg" alt="" />
        <p>
            Hanna's Story
        </p>
    </div>
  </div>
</div>

I want the img tag inside the '.item' which has the class of 'active' to be the background image of .testimonials also. 我希望具有“ active”类的“ .item”中的img标签也成为.testimonials的背景图像。 As each slide changes, the item image will change, and thus this background image should also change. 随着每张幻灯片的更改,项目图像也会更改,因此此背景图像也应更改。 How can I do this? 我怎样才能做到这一点?

The Slider is bootstrap's default carousel. 滑块是引导程序的默认轮播。 The code can be seen here: 该代码可以在这里看到:
http://codepen.io/anon/pen/aNjaEQ http://codepen.io/anon/pen/aNjaEQ

$(".item.active").each(function() {

  var $myBg  = $(".testimonial-bg", this),
      $myImg = $(".testimonial-img img", this);

  $myBg.css({backgroundImage: "url('"+ $myImg[0].src +"')"});

});

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

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