如何在打开新标签时保留标签(打开文件)?

时间:2017-04-12 03:11:32

标签: javascript php jquery html anchor

我有一个链接可以在浏览器中打开pdf。但是,它专注于新标签(带有pdf的标签。如何防止这种行为?

这是链接:

<strong><a style="text-decoration:none" class='row_link' href='javascript:void(0)' target="_blank" onFocus="false">Open File</a></strong>

这是jquery:

$("#myTable tbody").on('click','.row_link', function(e) {
    var no_s    = $(this).find('.filename').val().replace(/\//g , '');
    var b_url   = $('#base_url').val()+"assets/uploads/file/";

    var url     = b_url + no_s;
    window.open(url);
});

我尝试在锚中添加onfocus="false"。但是,它没有效果

4 个答案:

答案 0 :(得分:1)

&#13;
&#13;
<html>
<strong><a style="text-decoration:none" class='row_link' href='https://github.com/caiyongji' onclick="window.open('#','_blank');window.open(this.href,'_self');">Open File</a></strong>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

此效果在&#34;下称为&#34; pop。

基本上你需要在新打开的窗口上使用window.blur()来失去焦点,然后重新聚焦在现有的标签上。

$("#myTable tbody").on('click','.row_link', function(e) {
var no_s    = $(this).find('.filename').val().replace(/\//g , '');
var b_url   = $('#base_url').val()+"assets/uploads/file/";

var url     = b_url + no_s;
var newWindow = window.open(url);
newWindow.blur();
window.focus();
});

Relevant Stackoverflow post

答案 2 :(得分:0)

由于您需要打开背景标签的行为,请尝试以下代码,这是此answer

的示例

例如在你的jquery中;

$("#myTable tbody").on('click','.row_link', function(e) {
    var no_s    = $(this).find('.filename').val().replace(/\//g , '');
    var b_url   = $('#base_url').val()+"assets/uploads/file/";

    var url = document.createElement("a");
    url.href = b_url + no_s;
    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);
});

让我知道它是否有帮助,或者如果您发现问题

答案 3 :(得分:0)

如果我理解正确,您需要的是,您不希望向用户显示新打开的选项卡。这不能像@Rex建议的那样完成,但作为替代方案可以做的是,您可以打开一个弹出窗口并尽快将其最小化。

请查看以下代码:

父页面

<a href="javascript:openPopUP();">Click to open popup</a>

儿童页面

      <body onLoad="setTimeout('window.blur()', 1000);" 
      onFocus="setTimeout('window.blur()', 1000);">

您可以将时间1000更改为您想要的内容。

您可以在孩子身上使用此功能来关注父页面

window.parent.opener.focus(); 
相关问题