[英]Multiple anchor tag click on one JQuery event is not working
I have multiple anchor tag in a page.我在一个页面中有多个锚标记。 I want to click all the tag from JQuery to open each link in a new tab.我想单击 JQuery 中的所有标签以在新选项卡中打开每个链接。 But it is working only for the first element.但它仅适用于第一个元素。 Can anybody please help on this?有人可以帮忙吗? My attempt is below:我的尝试如下:
$('.tbl a').each(function () {
var url = $(this).attr("href");
window.open(url, '_blank');
})
NOTE: if I set background color in each it works well.注意:如果我在每个中设置背景颜色,效果很好。 Than why not new Tab?!!!比为什么不新标签?!!!
My suggestion from the comments would look something like this:我从评论中的建议看起来像这样:
Won't work here, because the sandoxed frame does not allow popups, but you get the idea.在这里不起作用,因为 Sandoxed 框架不允许弹出窗口,但你明白了。
$('.opener').on('click',function(){ $('ul a').each(function (index) { var url = $(this).attr("href"); window.open(url, '_blank'+index); }) })
.opener{ display:inline-block; background-color:#ccc; cursor:pointer; color:#FFF; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1>open new tabs</h1> <p>An unordered list:</p> <ul> <li><a href="https://www.coffe.com">Coffee</a></li> <li><a href="https://www.tea.com">Tea</a></li> <li><a href="https://www.milk.com">Milk</a></li> </ul> <p class="opener">test open </p>
Building on the answer in the comments,基于评论中的答案,
$('.tbl a').each(function () {
$(this).on("click", function(e){
window.open(e.target.href, '_blank');
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.