首页 > 怎样快速让网站的所有的a链接都在新窗口打开?

怎样快速让网站的所有的a链接都在新窗口打开?

产品让把网站中的所有a,都在新窗口打开,有什么快速的方法不一个一个写
target:_blank就能实现


产品的话也敢信,第二天就敢跟你说为什么点logo回首页也新窗口打开。

--update--
其实我是想说,虽然<base target="_blank"/>一步到位,但是,当产品再一张嘴的时候,想去掉一个就没那么简单了。


最好通过监听事件来解决 尽量不要循环 效率太低。

源生:

document.body.addEventListener(function(e) {
    if (e.target.nodeName.toUpperCase() === 'A' && e.target.href) {
        e.target.target = '_blank';
    }
}, true);

jQuery:

$('body').on('click', 'a', function(e) {
    e.target.target = '_blank';
});

使用

$(function(){
    $('body a').each(function(){
        $(this).attr('target','_blank');
    });
});

$(function(){
    var aLinkClickHandler=function(event){
        if($(this).attr('isReady')!=='1'){
            event.preventDefault();
            $(this).attr('target','_blank');
            $(this).attr('isReady','1');
            $(this)[0].click();
        }
    }
    $('body a').on('click',aLinkClickHandler);
});

$("a').attr("target","_blank");

<base target="_blank"/>
注意:是在head标签里添加
base最大的用处就是可以改变某一个网页默认的属性


用js写一个,然后放在头部


<base target="_blank" />

【热门文章】
【热门文章】