简体   繁体   English

无法在新标签页中打开链接

[英]not able to open links in a new tab

I need to verify a lot of links on a page. 我需要验证页面上的很多链接。 Rather than opening each link myself. 而不是自己打开每个链接。 This is what I did. 这就是我所做的。

jquerified the page using firequery plugin. 使用firequery插件对页面进行jquer化。 Then I typed following code in firebug. 然后我在萤火虫中键入以下代码。

a = $('a');
$.each(a, function(i,val){
  $val = $(val);
  $val.attr({target: '_blank'});
  $val.trigger('click');
});

Even though I am trigger click the links were not clicked. 即使触发我,也不会单击链接。 Why? 为什么?

You can do like this, ok you'll have problems with popup blockers but if this is only for debugging purposes you can simply disable blocker and that's it. 您可以这样做,好吧,您会遇到弹出窗口阻止程序的问题,但是,如果这仅是出于调试目的,您可以直接禁用阻止程序,仅此而已。

a = $('a');
$.each(a, function(i,val){
  window.open(val, '_blank');
});

Here is the whole code and it worked for me. 这是整个代码,对我有用。 Actually I didn't test it on a server, just checked html file on my desktop. 实际上,我没有在服务器上进行测试,只是在桌面上检查了html文件。 Firefox doesn't allow popups to display even I said to display popups but IE has option to allow popups for local files and it works, opens two windows for google and yahoo. 即使我说要显示弹出窗口,Firefox也不允许显示弹出窗口,但是IE可以选择允许本地文件弹出窗口,并且它可以工作,为google和yahoo打开两个窗口。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="jquery-min.js"></script>
    </head>
    <body>
        <a href="http://www.google.com">aa</a>
        <a href="http://www.yahoo.com">bb</a>
        <script>
            $(document).ready(function() {
                    a = $('a');
                    $.each(a, function(i,val){
                    window.open(val, '_blank');  
                });
            });
        </script>
    </body>
</html>

trigger('click') just doesn't work. trigger('click')无效。 I had the same problem recently and resolved it by using click() . 我最近遇到了同样的问题,并通过使用click()解决了它。

This code works for me: 该代码对我有用:

$("a").each(function(i, val) { window.open(val.href); });

However, Chrome blocks this code because it tries to open some 20 popups at once, but I can see that it does indeed try to open them. 但是,Chrome阻止了此代码,因为它试图一次打开约20个弹出窗口,但我可以看到它确实试图打开它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM