新标签中的javascript打开间接URL链接

时间:2017-10-16 04:06:04

标签: javascript url tabs indirect-objects

<script type="text/javascript"> 

    jQuery(document).ready(function($) {
      $('.tablepress-id-??').on('click', 'tr', function() {
        var $a = $(this).find('a').last();
        if ($a.length) window.location = $a.attr('href');
      });
    });

 </script>

当点击表格行中的任何地方时,上面的工作100%完美地打开网址链接[在每行中也使用图片中的网址链接] - 链接在相同的标签/窗口中打开但我需要它在新的标签/窗口中打开。

我已经阅读了一些类似的线程,但它们并不是完全相同的情况,我试图从这些线程中适应的任何东西都在工作[我可能会将其他代码放在我现有的错误位置?] < / p>

任何知道他们在这个领域做什么的人的帮助 - 即不是我:)都会非常感激......

3 个答案:

答案 0 :(得分:0)

if ($a.length) { 
  var url = $a.attr('href');
  window.open(url, '_blank');
}

答案 1 :(得分:0)

您可以在此处找到位置对象的一些有用文档:https://developer.mozilla.org/en-US/docs/Web/API/Window/location

正如你所看到的那样,

location.assign("http://www.mozilla.org"); // or
location = "http://www.mozilla.org";

但是对于assign函数,您可以将target属性'_blank'传递给新标签,如下所示:

window.location.assign('https://developer.mozilla.org/en-US/docs/Web/API/Window/location', '_blank')

答案 2 :(得分:0)

这对我有用,但是,我不知道代码,只是麻烦一些,因为我遇到了这个完全问题。

<script type="text/javascript"> jQuery(document).ready(function($){
    $('.tablepress-id-1').on( 'click', 'tr', function() {
        var $a = $(this).find('a').last(); 
        if ( $a.length ) 
        var url = $a.attr('href'); window.open(url, '_blank'); 
 } );
})
</script>