Jquery Modal对话框确认-新窗口倍增

发布于 2024-12-03 19:03:39 字数 1574 浏览 1 评论 0原文

我试图避免使用 jquery-ui 或 simple-modal 或任何插件。

我想要的功能是单击任何外部链接,显示一个包含“是”和“否”按钮的隐藏 div。如果用户单击“是”,他们将被带到一个新窗口。

我的问题是,这几乎有效,但例外的是,如果用户再次单击链接返回原始页面,则相同的链接将在两个选项卡中打开,如果您重复该链接将在三个选项卡中打开等...

<div id="overlay">
<div class="decoration">            
            <div class="overlay-content">
                <a href="#" class="close">X</a>
                <h1>You are now leaving the  website</h1>
                <p>This link will take you to a website where this Privacy Policy does not apply.</p>
                <p><strong>Select OK to continue.</strong></p>
                <a href="#" class="ok">OK</a>
                <a href="#" class="cancel">CANCEL</a>
            </div>
        </div>

$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").live('click', function (event) {

    var href_ext = $(this).attr("href");                             

    $('#overlay').fadeIn(500).css({'position':'fixed', 'top':'0px'});       

    $('#overlay .ok').live('click', function () {           
        window.open(href_ext);
        $('#overlay').hide();
        return false;
    });
    $('#overlay .close, #overlay .cancel').live('click', function () {                      
        $('#overlay').fadeOut(500);         
    });
    event.preventDefault();
});

以下是发生的情况的示例 http://jsbin.com/apekik/7

I'm trying to avoid using jquery-ui or simple-modal or any plugin.

The functionality I'm after is on click of any external link, reveal a hidden div containing yes and no buttons. If a user clicks yes then they are taken to a new window.

My problem is, this almost works, with the exception that if a user returns to the original page if they click the link again then the same links opens in two tabs and if you repeat the link opens in three tabs etc...

<div id="overlay">
<div class="decoration">            
            <div class="overlay-content">
                <a href="#" class="close">X</a>
                <h1>You are now leaving the  website</h1>
                <p>This link will take you to a website where this Privacy Policy does not apply.</p>
                <p><strong>Select OK to continue.</strong></p>
                <a href="#" class="ok">OK</a>
                <a href="#" class="cancel">CANCEL</a>
            </div>
        </div>

$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").live('click', function (event) {

    var href_ext = $(this).attr("href");                             

    $('#overlay').fadeIn(500).css({'position':'fixed', 'top':'0px'});       

    $('#overlay .ok').live('click', function () {           
        window.open(href_ext);
        $('#overlay').hide();
        return false;
    });
    $('#overlay .close, #overlay .cancel').live('click', function () {                      
        $('#overlay').fadeOut(500);         
    });
    event.preventDefault();
});

Here is an example of what's happening http://jsbin.com/apekik/7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-12-10 19:03:39

每次单击该链接时,都会再次调用该函数。每次调用该函数时,live 都会在所有链接上注册另一个回调,因此当用户最终单击“确定”时,window.open 函数将被调用反复叫唤。此外,fadeOut 被多次调用,但效果只是被隐藏,因为它正在淡出。

因此,我们将 .ok.close.cancel 的代码移至链接的点击处理程序之外,并更改 liveclick 应该没问题。

$('#overlay .ok').click(function (event) {   
     var href_ext = $(this).attr("href");          
     window.open(href_ext);
     $('#overlay').hide();
     return false;
});

$('#overlay .close, #overlay .cancel').click(function () {                      
    $('#overlay').fadeOut(500);         
});

$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").click(function (event) {
    $('#overlay').fadeIn(500).css({'position':'fixed', 'top':'0px'});       
    event.preventDefault();
});

Each time that the link is clicked, the function is called again. Each time that the function is called, live registers another callback on all of the links, so when the user finally click on "OK", the window.open function will be called repeatedly. Additionally, the fadeOut is being called multiple times, but the effects are just hidden because, well, it's fading out.

So, we move the code for .ok, .close and .cancel to outside of the link's click hander, and change live to click and it should be fine.

$('#overlay .ok').click(function (event) {   
     var href_ext = $(this).attr("href");          
     window.open(href_ext);
     $('#overlay').hide();
     return false;
});

$('#overlay .close, #overlay .cancel').click(function () {                      
    $('#overlay').fadeOut(500);         
});

$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").click(function (event) {
    $('#overlay').fadeIn(500).css({'position':'fixed', 'top':'0px'});       
    event.preventDefault();
});
萌辣 2024-12-10 19:03:39

感谢马克花时间研究这个问题并指出重复函数。您的解决方案没有完全按预期工作,因为链接引用了当前页面而不是外部链接本身。我仍然需要将所有其他功能与单击关联起来。我没有对代码进行太多更改,而是通过在 .live() 之前添加 .die() 来解决它,这可以防止您提到的重复函数。

$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").live('click', function (event) {
event.preventDefault();

    var href_ext = $(this).attr("href");                                 

    $('#overlay').fadeIn(500).css({'position':'fixed', 'top':'0px'});       

    $('#overlay .ok').die().live('click', function () {           
        window.open(href_ext);
        $('#overlay').hide();
        return false;
    });

    $('#overlay .close, #overlay .cancel').click(function () {                      
        $('#overlay').fadeOut(500);         
    });
});

工作解决方案: http://jsbin.com/apekik/14

Thank you Mark for taking the time to look into this and for pointing out the repeating function. Your solution didn't quite work as intended because the link references the current page rather than the external link itself. I still needed to associate all the other functionality with the click. I didn't change my code too much and solved it by adding .die() before .live() which prevents the repeating function you mentioned.

$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").live('click', function (event) {
event.preventDefault();

    var href_ext = $(this).attr("href");                                 

    $('#overlay').fadeIn(500).css({'position':'fixed', 'top':'0px'});       

    $('#overlay .ok').die().live('click', function () {           
        window.open(href_ext);
        $('#overlay').hide();
        return false;
    });

    $('#overlay .close, #overlay .cancel').click(function () {                      
        $('#overlay').fadeOut(500);         
    });
});

Working solution: http://jsbin.com/apekik/14

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文