如何打开新窗口/选项卡,但专注于打开窗口

How to open new window/tab but focus on opener window?

本文关键字:专注 开窗口 于打 选项 新窗口 何打开 窗口      更新时间:2023-09-26

我想模拟ctrl+click交互,在所有浏览器的后台选项卡中打开一个链接。

略有不同的是,链接是基于选中的复选框。

这是JSbin+问题的代码,供将来参考:

JSBin链接

http://jsbin.com/hisozawibu/2/

代码

<input class="ad-check" data-url="http://www.google.com" type="checkbox" id="test" name="test" />
<label for="test">Test</label>
$('body').on('click', '.ad-check' ,function(){
    var self = $(this);
    window.open(self.attr('data-url'), "_blank");
    window.blur();
});

我看过图书馆,但这么简单的东西似乎没有必要。

当你搜索酒店并选择供应商时,我想掌握的技巧可以在Kayak上看到。

http://www.kayak.co.uk/hotels

B

注意,利用这个http://jsfiddle.net/azproduction/LRc7Z/图案

尝试

html

<input class="ad-check" data-url="http://www.google.com" type="checkbox" id="test" name="test" />
<label for="test">Test</label>
<a id="ad-check-link" href=""></a>

js

var simulateClick = function (ctrl, shift, isMiddle) {
    var evt = document.createEvent('MouseEvents');
    evt.initMouseEvent('click'
                       , true, true, window
                       , 0, 0, 0, 0, 0, ctrl
                       , false, shift, false
                       , isMiddle ? 1 : 0, null );
    document.getElementById('ad-check-link').dispatchEvent(evt);
};
// substituted `.one()` for `.on()` to prevent calling action
// if clicked second time, after being initially checked by first click
$('body').one('click', '.ad-check' ,function(){
    var self = $(this);
    var link = $("a#ad-check-link")
               .prop("href", self.attr('data-url'))[0];
    link.onclick = simulateClick(true, false, false);
});

jsfiddlehttp://jsfiddle.net/guest271314/awjvbLt5/1/

这正是您所需要的。

http://jsfiddle.net/5fz2u6Lq/

$('body').on('click', '.ad-check' ,function(){
    var self = $(this);
    var win = window.open(self.attr('data-url'), "_blank", 'modal=yes');
    win.blur();
});

编辑:

如果你也把注意力集中在旧的上呢。

http://jsfiddle.net/5fz2u6Lq/1/

$('body').on('click', '.ad-check' ,function(){
    var self = $(this);
    var current = window;
    var win = window.open(self.attr('data-url'), "_blank", "modal=yes");
    win.blur();
   current.focus();
});