首页 > 解决方案 > 仅使用 Jquery 将链接分配给图像并使其可点击

问题描述

我有一个与一些链接链接的图像滑块,在桌面版本中,当您单击图像时,它会正确定向到链接,但是当我切换到移动版本并单击图像时,链接不起作用,我尝试使用此 Jquery 解决此问题:

jQuery (document) .ready (function () {
jQuery ('. fullwidthbanner-container ul> li: nth-child (3) img'). each (function () {
var currentImage = jQuery (this);
currentImage.wrap ("<a target='_self' href='http://jquerybyexample.blogspot.com'" + currentImage.attr("href") + "'</a>");
});
});

但它仍然没有工作,有没有办法让这些图像超链接?

标签: javascripthtmljquerycssmagento

解决方案


建议尝试以下方法。

jQuery(function($) {
  $('.fullwidthbanner-container > ul > li:eq(3) > img').each(function(i, el) {
    $(el).wrap(function() {
      return "<a href='http://jquerybyexample.blogspot.com" + $(this).attr("src") + "'></a>";
    });
  });
});

查看更多:https ://api.jquery.com/wrap/

图像通常具有src属性而不是href.


推荐阅读