简体   繁体   English

如何使用jQuery选择器,获取图像的src并将其设置为另一个元素的背景图像?

[英]How to jQuery selectors, get an image's src and set it as a background-image of another element?

I'm trying to make this work with the following scenario: 我正在尝试在以下情况下实现此目的:

My goal is to get the first image's src from the html structure bellow and then set its src value as the background-image: of another element which exists in the DOM. 我的目标是从下面的html结构中获取第一个图像的src,然后将其src值设置为DOM中存在的另一个元素的background-image:

The HTML output, dynamically loaded. HTML输出,动态加载。

I've set up the Instafeed jQuery plugin in a way it outputs the following html structure: Images are dynamically loaded through Instagram API. 我已经以输出以下html结构的方式设置了Instafeed jQuery插件:通过Instagram API动态加载图像。

I want to grab the image src from the first image bellow... 我想从下面的第一个图像中获取图像src ...

<div id="instafeed">
<div class="col-xs-4">
 <a href="#" target="_blank" class="thumbnail fa-eye fa">
  <img class="dynamic-image" src="https://scontent.cdninstagram.com/hphotos-xpa1/t51.2885-15/e15/10375586_312675495555582_308260980_n.jpg">
 </a>
</div>

<div class="col-xs-4">
 <a href="#" target="_blank" class="thumbnail fa-eye fa">
  <img class="dynamic-image" src="https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/e15/10454019_1429094704028916_1903084125_n.jpg">
 </a>
</div>

<div class="col-xs-4">
 <a href="#" target="_blank" class="thumbnail fa-eye fa">
  <img class="dynamic-image" src="https://scontent.cdninstagram.com/hphotos-xpf1/t51.2885-15/e15/1530818_682625665122870_298851871_n.jpg">
 </a>
</div>
</div>

...And place it as the url of the following target element .bgcover : ...并将其放置为以下目标元素.bgcover

<div class="masthead bgcover" style="background-image: url('https://distilleryimage11-a.akamaihd.net/fdc05880f98511e2af2c22000a9e510c_7.jpg');"></div>

jQuery jQuery的

The code bellow works, however, it gets the img src of one of the images in the html out and not the first one... 下面的代码可以工作,但是它可以获取html中图像之一的img src而不是第一个图像...

$( window ).load(function() {
// For each image with the class .dynamic-image on the page
$(".dynamic-image").each(function() {
// Grab the image
var image_source = $(this).attr('src');

// Set the background
$(".bgcover").css("background-image",'url('+image_source+')');

 });
});

By reading the jQuery's :eq() Selector documentation, I'm now trying to learn how to use the selectors, but I'm pretty new to JS/jQuery, so I'd really appreciate any hints about making it work the right way. 通过阅读jQuery的:eq()选择器文档,我现在正在尝试学习如何使用选择器,但是我对JS / jQuery还是很陌生,所以我非常感谢任何有关使其正确工作的提示。 。

The .each() function in jquery is like a loop. jQuery中的.each()函数就像一个循环。 It will run loop through for every match. 它将为每个匹配循环运行。 So, in your above code, ideally it is going to pick the last img tag and set its image as the background. 因此,在上面的代码中,理想情况下,它将选择最后一个img标签并将其图像设置为背景。

Instead if you used .first() function it would return the first element that it matches. 相反,如果使用.first()函数,它将返回它匹配的第一个元素。 This is the implementation that would work for you. 这是适合您的实现。

$(window).load(function () {
    // For each image with the class .dynamic-image on the page

    // Grab the image
    var image_source = $(".dynamic-image").first().attr('src');
    image_source = "url('" + image_source + "')";
    // Set the background
    $(".bgcover").css("background-image", image_source);

});

Hope this helps. 希望这可以帮助。

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

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