用点击图像的src替换特色图像的src [英] Replace a featured image's src with the src of the image clicked on

查看:133
本文介绍了用点击图像的src替换特色图像的src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.gallery div中点击的任何图片替换页面上的精选图片。这将在我的网站中的多个页面上使用,所以我希望它与图库中的图像数量无关(因为它会有所不同)以及图库中图像的位置。

I'm attempting to replace a featured image on a page with any image clicked on in the .gallery div. This will be used on multiple pages in my site so I'd like it to be agnostic of the number of images in the gallery (as that will vary) and where in the gallery the image falls.

有没有办法简单地抓住点击图像的src并用它替换另一个图像的src?我已经在Stack Overflow上挖掘了一些其他主题,并将基于它们的以下代码拼凑在一起,但它似乎还没有工作。我是朝着正确的方向前进吗?

Is there a way to simply grab the src of the clicked image and replace the src of another image with it? I've dug through a few other topics here on Stack Overflow and pieced together the following code based on them, but it doesn't seem to be working yet. Am I headed in the correct direction?

$(".galleryimg").click(function(){
    var r1 = $(this).attr("src");
    $('#selected-image').attr("src", r1).replace(r1);
});


推荐答案

这应该有效:

$(".galleryimg").click(function(){
    var r1 = $(this).attr("src");
    $('#selected-image').attr("src", r1);
});

如果没有,则代码或DOM中的其他位置会出错。

If it doesn't, there is an error somewhere else in your code or DOM.

这是一个简短的演示

Here is a brief demo

这篇关于用点击图像的src替换特色图像的src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆