新标签打开时防止文档失去焦点 [英] Prevent document losing a focus when new tab is opened

查看:126
本文介绍了新标签打开时防止文档失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(e).append(
$(< ; a>)。attr('href',url)
.attr('target','_blank')
.text(text)
);

如何防止文档在新标签打开时失去焦点? b

我想要在后台打开新选项卡,而不必将焦点切换到其中。



<$ p $在这个问题上被接受的答案: p> $( '一个[目标= _空白]')removeAttr( '目标')。

使用相同问题的最上面的答案,试试这个:&b
$ b

 < a href =your_urlonclick =window.open('#','_ blank'); window.open(this.href,'_ self' );> 

使用问题被接受的答案,试试这个:

Fiddle。
$ b

 函数openNewBackgroundTab(){
var a = document.createElement(a);
a.href =http://www.google.com/;
var evt = document.createEvent(MouseEvents);
// initMouseEvent的第十个参数设置ctrl键
evt.initMouseEvent(click,true,true,window,0,0,0,0,
true,false, false,false,0,null);
a.dispatchEvent(evt);



$ b

这里第十个参数设置 ctrl 键是正确的。然后,就像 ctrl + 点击,然后在另一个窗口中打开,而不会丢失焦点。


Let's have a simple code like this:

$(e).append(
  $("<a>").attr('href', url)
          .attr('target', '_blank')
          .text(text)
);

How can I prevent document losing a focus when new tab is opened?

I want a new tab to be opened in background without switching a focus into it.

解决方案

Try using this javascript by using this question's accepted answer:

$('a[target="_blank"]').removeAttr('target');

Using the same question's most upvoted answer, try this aswell:

<a href="your_url" onclick="window.open('#','_blank');window.open(this.href,'_self');">

Using this question's accepted answer, try this:

Fiddle.

function openNewBackgroundTab(){
    var a = document.createElement("a");
    a.href = "http://www.google.com/";
    var evt = document.createEvent("MouseEvents");
    //the tenth parameter of initMouseEvent sets ctrl key
    evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
                                true, false, false, false, 0, null);
    a.dispatchEvent(evt);
}

Here the tenth parameter sets the ctrl key to be true. Then that just acts like a ctrl + click and then that's opened in an another window without losing focus.

这篇关于新标签打开时防止文档失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆