【问题标题】:Jquery open href if src=如果 src= 则 Jquery 打开 href
【发布时间】:2018-05-30 23:46:40
【问题描述】:

我正在尝试使用 Jquery 在图像表中搜索与 src 中定义的参数匹配的任何图像,如果找到,则在新窗口中打开 href。 href 位于网站代码中的 img src 之前,我相信我已经在我的 Jquery 中对此进行了补偿。

不幸的是,我不确定为什么 Jquery 无法正常工作 - 有人可以看看它并在正确的方向上推动我吗?我怀疑问题出在打开指定的href,但对于我来说,我无法发现问题。提前致谢。

$('img[src$=random.gif]').each(function() {
      $img = $(this);
      $link = $url.closest('a');
      var href = $link.attr('href');
        var win = window.open.attr('href', '_blank');
        win.focus();
      });
    setTimeout(function () { window.location.href = window.location.href }, 500);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td>
<a href="/link">
<img src="/random.gif">
</a>
</td>

【问题讨论】:

    标签: jquery


    【解决方案1】:

    如果$img$(this) 图像 而不是$url.closest('a'); 你应该指向$img

      $img = $(this);
      $link = $img.closest('a');
    

    编辑示例:https://jsfiddle.net/4ua4p5pf/

    $('img[src$="random.gif"]').each(function() {
    
        var $img = $(this);
        var href = $img.closest('a').attr('href');
    
        setTimeout(function () {
           var win = window.open( href, "Image description", "status,resizable,scrollbars");
           // var win = window.open( href, "image"); // open in new tab
           win.focus();
        }, 500);
    
    });
    

    https://developer.mozilla.org/en-US/docs/Web/API/Window/open
    window.open with target "_blank" in Chrome

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 2011-09-26
      相关资源
      最近更新 更多