【问题标题】:Assign links to an image and make them clickable, using only Jquery仅使用 Jquery 将链接分配给图像并使其可点击
【发布时间】:2020-04-28 02:05:54
【问题描述】:

我有一个与一些链接链接的图像滑块,在桌面版本中,当您单击图像时,它会正确定向到链接,但是当我切换到移动版本并单击图像时,链接不会工作,我尝试使用此 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>");
});
});

但还是不行,有没有办法让这些图片超链接?

【问题讨论】:

  • 欢迎来到 Stack Overflow。请提供一个最小的、可重复的示例:stackoverflow.com/help/minimal-reproducible-example
  • 请连同这个js一起提供html。从层次结构定位是非常危险的,我会推荐几乎任何替代方案。您是否无法向链接添加类名或属性?除了 hacky JS 解决方案之外,可能还有更好的方法让链接在移动设备上也能正常工作。

标签: javascript html jquery css magento


【解决方案1】:

建议尝试以下方法。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 2016-08-07
    • 2014-12-12
    • 2019-06-02
    • 2013-09-01
    • 2021-05-15
    相关资源
    最近更新 更多