如何正确打开链接数组?

时间:2018-01-10 22:08:18

标签: javascript jquery

我有一个问题,如何同时打开阵列中的所有链接,不仅仅是像现在这样,我尝试过这样的事情:



var urls = ["https://www.google.com", "https://www.facebook.com"];
let main = function() {
	for (var i = 0; i < urls.length; i++) {
		window.open(urls[i], '_blank');
	}
};
main();
&#13;
&#13;
&#13;

有人可以展示一些例子吗?谢谢! :P

1 个答案:

答案 0 :(得分:0)

<html>
    <head>
        <script type='text/javascript'>
            var urls = ["https://www.google.com", "https://www.facebook.com"];
            function main() {
                for (var i = 0; i < urls.length; i++) {
                    var el = document.querySelector('#hidden') ;
                    //window.open(urls[i], '_blank');
                    var a = document.createElement('a');
                    a.href = urls[i];
                    a.target = '_blank';
                    el.appendChild(a);
                    a.click();
                }
            }
            window.addEventListener('load', function () { main();}, !1);
        </script>
    </head>
    <body>
        <div id='hidden'></div>
    </body>
</html>