简体   繁体   English

抓取href并将其放在src上

[英]Grab href and put it on src

I have a div that brings a bunch of thumbnails with href links to the full image. 我有一个div,它带来了一堆带有href链接的缩略图到完整图像。 So instead of opening this image away from the site, I need to bring this image dynamically in a second div. 因此,除了需要在远离站点的位置打开该图像外,我还需要在第二个div中动态添加该图像。

Here's what I have 'til now: 这是我直到现在的内容:

  • The HTML HTML

     <article id="thumbnails"> <a href="large-image-1.jpg"> <img class="image-99" alt="97" src="thumbnail-1.jpg"> </a> <a href="large-image-2.jpg"> <img class="image-98" alt="96" src="thumbnail-2.jpg"> </a> <a href="large-image-3.jpg"> <img class="image-97" alt="95" src="thumbnail-3.jpg"> </a> </article> 
     <div id="large-image"> <img id="xxl" src="" title=""> </div> 
  • the jQuery jQuery的

     $(document).ready(function() { $('#thumbnails').on('click','a', function(event) { var toLoad = $(event.target).append('<img src="' + 'attr('src').jpg />"'); $('#large-image').fadeOut('fast',loadContent); function loadContent () { $('#large-image').load(toLoad,'',showNewContent); } function showNewContent () { $('#large-image').fadeIn('normal'); } return false; }); 

I also did a codepen of it to illustrate it better. 我也做了一个代码笔,以更好地说明它。

One thing I'm considering as an alternative is to record the clicked image href and replace the src of the other div with the attr() thing. 我正在考虑的一种替代方法是记录单击的图像href并将另一div的src替换为attr() But I got lost in how to do that. 但是我迷失了如何做到这一点。

You can also try to get the link's href and change the target-image's src . 您也可以尝试获取链接的href并更改目标图像的src

var largeImage = $('#large-image img');

$('#thumbnails a').click(function(e){
  e.preventDefault();

  var imageUrl = $(this).attr('href'); 

  largeImage.attr('src', imageUrl);
});

Example on JS Bin JS Bin上的示例

$('#thumbnails').on('click','a', function(event) {
    var toLoad = $(this).append('<img src="' + this.href + '.jpg" />');

    //Rest of code
});

EDIT: The above code will get the href attribute of the clicked link and adds it to the src of the image. 编辑:上面的代码将获取单击的链接的href属性,并将其添加到图像的src。

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

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