简体   繁体   中英

How to make an image link to the post?

My posts have an image in them. They are shown in the homepage of my site. Each post has a picture, a description about the post, and a "more" button which takes the user to the full article.

My issue is that I want to make the image link to the full post. I want the image to function as the "more" button.

I have tried using jQuery, wrap() function, linking the image in the article itself, nothing has worked out so far.

The code for the image is just:

<img src="random.jpg" class="YTimgs">

Does anybody know how I can get this to work? I have been working on this issue and have not found the correct solution. I hope someone helps me out.

Some of your images have class YTimg , others have YTimgs ... give it only one class:

$(document).on("click", "img.YTimgs", function(){
    $(this).closest("div.post").find(".jump-link").find("a").trigger("click");
});

And this should do the tricky

Updated fiddle: http://jsfiddle.net/5md7p9L7/7/

<img src="http://lorempixel.com/400/200/"/>
<a class="link" href="http://google.com" target="_blank">The picture and this text are both links to the same page :)</a>

jQuery

$('img').click(function(){
    var link =$(this).next().attr('href');
    window.open(link);   
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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