我的帖子中有图片。它们显示在我网站的主页上。每篇文章都有一张图片、关于该文章的描述以及一个“更多”按钮,可将用户带到完整的文章。
我的问题是我想将图像链接到完整的帖子。我希望该图像充当“更多”按钮。
我尝试过使用 jQuery、wrap() 函数,链接文章本身中的图像,但到目前为止还没有任何结果。
图像的代码如下:
<img src="random.jpg" class="YTimgs">
有人知道我怎样才能让它工作吗?我一直在研究这个问题,但没有找到正确的解决方案。我希望有人能帮助我。
最佳答案
更新 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);
});
关于javascript - 如何为帖子添加图片链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27985185/