与target =“_blank”链接在Chrome的新标签中无法打开 [英] link with target="_blank" does not open in new tab in Chrome

查看:772
本文介绍了与target =“_blank”链接在Chrome的新标签中无法打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此网站上,顶级手风琴导航包含一个名为Foundation的元素:(屏幕截图)



此元素由HTML代码生成:

 < a href =http://www.foracure.org .autarget =_ blankstyle =width:105px;>< / a> 

但是,在Chrome中,当您点击此元素时,新网站不会以新的方式打开标签。



你能告诉我为什么吗?谢谢。

解决方案

因为JavaScript正在处理click事件。当您点击时,会调用以下代码:

  el.addEvent('click',function(e){
if(obj.options.onOpen){
new Event(e).stop();
if(obj.options.open == i){
obj.options.open = null;
obj.options.onClose(this.href,i);
} else {
obj.options.open = i;
obj.options.onOpen(this。 href,i);
}
}
})

onOpen 手动更改位置



编辑:关于你的评论...
如果你可以修改ImageMenu.js,你可以更新脚本,调用 onClose 来传递 a 元素对象( this ,而不是 this.href

  obj.options.onClose(this,i); 

然后用下面的 onOpen change:

  window.addEvent('domready',function(){
var myMenu = new ImageMenu $$('#imageMenu a'),{
openWidth:310,
border:2,
onOpen:function(e,i){
if(e.target = =='_blank'){
window.open(e.href);
} else {
location = e.href;
}
}
});
});

这将检查元素的目标属性,看它是否为 _blank ,然后调用 window.open ,如果找到。



如果你愿意不要修改ImageMenu.js,另一个选择是修改链接以在 onOpen 处理程序中标识它们。比如说:

 < a href =http://www.foracure.org.au/#_b=1 target =_ blankstyle =width:105px;>< / a> 

然后,将 onOpen 调用更新为:

  onOpen:function(e,i){
if(e.indexOf('_ b = 1')> ; -1){
window.open(e);
} else {
location = e;






$ p

唯一的缺点是用户看到散列最后,如果您计划在新窗口中打开的链接数量较少,则可以创建一张地图并对照该地图进行检查。例如:

  var linksThatOpenInANewWindow = {
'http://www.foracure.org.au':1
};

onOpen:function(e,i){
if(linksThatOpenInANewWindow [e] === 1){
window.open(e);
} else {
location = e
}
}

其他人建议修改链接(使用 c $ c>或 javascript:)并添加一个内联事件处理程序( onclick ) - 我不建议因为它在JS被禁用/不支持时断开链接。



祝你好运!


On this website, the top accordion navigation contains an element called "Foundation": (screenshot).

This element is produced by HTML code:

<a href="http://www.foracure.org.au" target="_blank" style="width: 105px;"></a>

However, in Chrome, when you click on this element, the new website does not open in a new tab.

Can you please tell me why? Thank you.

解决方案

Because JavaScript is handling the click event. When you click, the following code is called:

el.addEvent('click', function(e){
    if(obj.options.onOpen){
        new Event(e).stop();
        if(obj.options.open == i){
            obj.options.open = null;
            obj.options.onClose(this.href, i);
        }else{
            obj.options.open = i;
            obj.options.onOpen(this.href, i);
        }   
    }       
})

The onOpen manually changes the location.

Edit: Regarding your comment... If you can modify ImageMenu.js, you could update the script which calls onClose to pass the a element object (this, rather than this.href)

obj.options.onClose(this, i);

Then update your ImageMenu instance, with the following onOpen change:

window.addEvent('domready', function(){
    var myMenu = new ImageMenu($$('#imageMenu a'), {
        openWidth: 310,
        border: 2,
        onOpen: function(e, i) {
            if (e.target === '_blank') {
                window.open(e.href);    
            } else {
                location = e.href;
            }
        }
    });
});

This would check for the target property of the element to see if it's _blank, and then call window.open, if found.

If you'd prefer not to modify ImageMenu.js, another option would be to modify your links to identify them in your onOpen handler. Say something like:

<a href="http://www.foracure.org.au/#_b=1" target="_blank" style="width: 105px;"></a>

Then, update your onOpen call to:

onOpen: function(e, i) {
    if (e.indexOf('_b=1') > -1) {
        window.open(e);   
    } else {
        location = e;
    }
}

The only downside to this is the user sees the hash on hover.

Finally, if the number of links that you plan to open in a new window are low, you could create a map and check against that. Something like:

var linksThatOpenInANewWindow = {
    'http://www.foracure.org.au': 1
};

onOpen: function(e, i) {
    if (linksThatOpenInANewWindow[e] === 1) {
        window.open(e);   
    } else {
        location = e
    }
}

Only downside is maintenance, depending on number of links....

Others have suggested modifying the link (using # or javascript:) and adding an inline event handler (onclick) - I don't recommend that at all as it breaks links when JS is disabled/not supported.

Good luck!

这篇关于与target =“_blank”链接在Chrome的新标签中无法打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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