本文介绍了用jQuery打开多个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我觉得我已经在这里做了我的尽职调查...... JSFIddle -



我正在尝试创建一个可以弹出多个选项卡的页面。我明白,使用此代码...

 < a href =google.comtarget =_ blank>新标签页< / a> 

会弹出 1 新标签页。但是,正如 stackoverflow q / a中所解释的那样,它需要成为Chrome的用户发起的事件,以便弹出新选项卡而不是窗口。由于这个要求,一个循环会弹出一个新选项卡,然后为每个后续链接弹出新窗口。

  jQuery(a。 (#arguments)。val(); 
jQuery(this).attr(href)+/+ jQuery(#arguments)。 (this).attr(href,string);
jQuery(this).trigger('click');
});

我尝试了编程创建链接并点击它们,使用不同的插件,超时,方法,甚至我尝试菊花链的过程,并在页面加载时触发它(一个巨大的PHP / GET变量/页面加载触发器事件),但它仍然会弹出窗口,因为事件不是用户启动的。



我试过 Q / A(使用jQuery.get()打开一个选项卡并写入它),但看到它仍然调用window.open(),我觉得我仍然会遇到完全相同的循环问题。






好的。除了它是由你的浏览器控制的之外,还有真正的答案吗?我觉得一定有办法。谢谢,对不起, 打开新标签:{在chrome上测试}



  $('a.site')。each(function(){
var clk = document.createEvent(MouseEvents);
clk.initMouseEvent(click,false,true,window,0,0,0,0,0,true,false,false,true, 0,null);
this.dispatchEvent(clk);
});


Ok, I feel I have done my due diligence here... JSFIddle - http://jsfiddle.net/taytayevanson/8BpHw/5/

I am trying to create a page that will pop multiple tabs. I understand that using this code...

<a href="google.com" target="_blank">New Tab</a>

will pop 1 new tab. However, as explained in this stackoverflow q/a, it needs to be a "user initiated event" for Chrome to pop a new tab and not a window. Because of this requirement, a loop will pop 1 new tab and then new windows for each following link.

jQuery("a.site").each(function(){
    var string = jQuery(this).attr("href") + "/" + jQuery("#arguments").val();
    jQuery(this).attr("href",string);
    jQuery(this).trigger('click');
});

I have tried programmatically creating links and clicking them, with different plugins, timeouts, methods, and I even tried "daisy-chaining" the process and firing it on a page load (a huge PHP/GET variable/page load trigger thing) but it would still pop windows because the event was not user initiated.

I tried this...

function clickLink(link) {
var cancelled = false;

if (document.createEvent) {
    var event = document.createEvent("MouseEvents");
    event.initMouseEvent("click", true, true, window,
        0, 0, 0, 0, 0,
        false, false, false, false,
        0, null);
    cancelled = !link.dispatchEvent(event);
}
else if (link.fireEvent) {
    cancelled = !link.fireEvent("onclick");
}

if (!cancelled) {
    window.location = link.href;
}
}

and although I can read it, I don't understand it well enough to comprehend what i'm supposed to pass into this function. I tried something like this...

jQuery("a.site").each(function(){
    var string = jQuery(this).attr("href") + "/" + jQuery("#launcher").val();
    jQuery(this).attr("href",string);
    clickLink(jQuery(this));
});

But I get a "object has no method 'dispatchEvent'" console error. I tried using that same "var event" and just calling...

link.trigger(event);

but there was a console error there as well. The only thing I have not tried is in this Q/A (using jQuery.get() to open a tab and write to it) but seeing as it still calls window.open(), I feel like i'll still run into the exact same loop issue.


Ok. Got all that out of the way... Is there a real answer for this besides "it's controlled by your browser" ? I feel like there must be a way. Thank you, sorry for the novel :)

解决方案

See using dispatchEvent to open new tab: {tested on chrome}

DEMO

$('a.site').each(function () {
    var clk = document.createEvent("MouseEvents");
    clk.initMouseEvent("click", false, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
    this.dispatchEvent(clk);
});

这篇关于用jQuery打开多个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:26