开发者

not able to open links in a new tab

开发者 https://www.devze.com 2022-12-22 22:00 出处:网络
I need to veri开发者_JAVA技巧fy a lot of links on a page. Rather than opening each link myself. This is what I did.

I need to veri开发者_JAVA技巧fy 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号