简体   繁体   中英

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. 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. 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.

<!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. I had the same problem recently and resolved it by using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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