Replace a featured image's src with the src of the image clicked on

…衆ロ難τιáo~ 提交于 2019-12-11 13:17:42

问题


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.

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

回答1:


This should work:

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

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

Here is a brief demo



来源:https://stackoverflow.com/questions/23354602/replace-a-featured-images-src-with-the-src-of-the-image-clicked-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!