如何实现点击链接 A 弹出窗口 X,点击链接 B 继续在弹出窗口 X (刷新)打开?


就是有很多链接,点击链接会弹出窗口,如何实现点击不同的链接,始终在同一弹出窗口中打开,而不是每次都弹出新的窗口。

用下面的代码只能每次都弹出新的窗口。


 $('a').click(function(){
    window.open(this.href, "");
    return false;
});

php JavaScript

红豆黑米粥 8 years, 7 months ago

为什么用 js ? 这样做很多浏览器会默认阻止。 <a> 默认就是在当前窗口打开
代码:


 $('a').click(function(){
    location.href = this.href;  //可以后退到当前页
    // 或者 location.replace(this.href) // 不可以回退到当前页
    return false;
});

gwat0 answered 8 years, 7 months ago


 var x;
$('a').click(function(){
    if(x){
        x.location.href = this.href;
    } else {
        x = window.open(this.href, '');
    }
    return false;
});

现在就按下F12,执行代码,点链接试试。


2015-9-6 更新:如果弹出的窗口关闭则重新打开


 var x;
$('a').click(function() {
    if (!x || x.closed || !x.opener) {
        x = window.open(this.href, '');
    } else {
        x.location.href = this.href;
    }
    return false;
});

NEET面瘫 answered 8 years, 7 months ago

Your Answer