如何使用jQuery脚本一次打开多个链接

时间:2017-07-29 18:38:39

标签: javascript jquery

我有一个客户图表,我需要通过“在新标签页中打开”来手动打开每个客户

我正在尝试使用jQuery脚本,这样当我在控制台中输入脚本时,它将为我打开该页面中的每个客户链接。

以下是我现在的代码。此代码的作用在某种程度上,它只打开客户页面中50的第一个客户链接。任何人都可以帮我配置它,以便在给定页面的新标签中打开每个客户吗?

enter image description here

(我使用最新版本的Chrome作为我的浏览器)

var $pbfLinks = $('.name a');
     for (var i = 0; i < $pbfLinks.length; i++) {
          var element = $pbfLinks[i];
          var $pbfLink = $(element);
             $pbfLink.attr('target', '_blank');
             $pbfLink[i].click(function() {
                     window.open($pbfLink);
  });
};



 <tbody>
                          <tr data-bind-class="{selected: bulkOperations.selected[6463344]}"
                              data-define="{nestedLinkContainer: new Shopify.NestedLinkContainer(this)}">
                            <td class="select">
                              <div class="next-input-wrapper"><label class="next-label helper--visually-hidden next-label--switch" for="customer_ids_6463344">Select customer, Mayra </label><input type="checkbox" name="customer_ids_6463344" id="customer_ids_6463344" value="6463344" bind="bulkOperations.selected[6463344]" bind-event-change="bulkOperations.selectionChanged()" class="next-checkbox" /><span class="next-checkbox--styled"><svg class="next-icon next-icon--size-10 checkmark"> <use xlink:href="#next-checkmark-thick" /> </svg></span></div>
                            </td>
                            <td class="name no-wrap">
                              <a data-nested-link-target="true" href="/admin/customers/6463344">Mayra </a>
                                                          </td>
                            <td class="location type--subdued">
                                 US
                            </td>
                            <td class="orders tc">0</td>
                            <td class="last_order tc">
                                &mdash;
                            </td>
                            <td class="money_spent tr">$0.00</td>
                          </tr>
                          <tr data-bind-class="{selected: bulkOperations.selected[6463317764]}"

3 个答案:

答案 0 :(得分:1)

根据您提出的问题所提供的信息,您可以通过这种方式实现目标。

  var links = $('a[data-nested-link-target="true"]');	
	Array.from(links).forEach(function(link){
		window.open(link.href, '_blank')		
	});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<a data-nested-link-target="true" href="http://www.google.com">Google </a>
<a data-nested-link-target="true" href="http://www.facebook.com">Facebook </a>
<a data-nested-link-target="true" href="http://www.twitter.com">Twitter </a>	
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
	
</body>
</html>

这是基于所有客户链接都具有data-nested-link-target="true"属性的假设; 这是上面代码https://jsbin.com/dejotol/edit?html,js,output

的jsbin

答案 1 :(得分:0)

您可以使用此代码:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script> 
$(document).ready(function() {
    var linksArray =['https://www.google.com','http://www.facebook.com','http://www.stackoverflow.com'];
    var i;
    $('#open').click(function() {
      for( i=0; linksArray.length > i; i++){
        window.open(linksArray[i]);
      }
    });
});
</script>
</head>
<body>

<button id="open" class="button">Open Tabs</button>

</body>
</html>

答案 2 :(得分:0)

您根据要求发布的HTML代码看起来不完整。希望结构有点像

<div class="name"...>
<tr>
<td>
<a href="...>
</td>

试试这个:

$('.name > tr > td').each(function() {
    var $this = $(this > a);
    $this.attr('target', '_blank');
    $this.click(function() {
        window.open($this.attr('href'));
        window.focus();
        return false;
  });
});
相关问题