简体   繁体   English

单击<a>标签时,显示一个覆盖并遵循href URL(</a> <a>如果选择了“是”作为选项)</a>

[英]On click of an <a> tag, show an overlay and follow the href URL of <a> if Yes is selected as an option

I have a requirement to show an overlay on click of a anchor tag. 我需要在点击锚标记时显示覆盖图。 If affirmative YES response is selected from the overlay options, the default <a> tag behaviour should follow with the href link opening in new window/current window as required. 如果从覆盖选项中选择了肯定的YES响应,则默认的<a>标记行为应跟在href链接之后,并根据需要在新窗口/当前窗口中打开href链接。

My jquery to show an overlay on click of tag: 我的jQuery显示点击标记时的覆盖:

$('a.external-link').click(function() {

var popupid = "popuprel";

$('#' + popupid).fadeIn();

$('body').append('<div id="fade"></div>');
$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();

var popuptopmargin = ($('#' + popupid).height() + 10) / 2;
var popupleftmargin = ($('#' + popupid).width() + 10) / 2;

$('#' + popupid).css({
'margin-top' : -popuptopmargin,
'margin-left' : -popupleftmargin
});

return false;
});

Yes option click function call: 是的选项单击函数调用:

$('.link-continue').click(function() {                          
$('#fade , #popuprel').fadeOut()  
});

<a class = "link-continue">YES</a>

Currently on click of the YES button the overlay closes but how should i redirect it to the URL('href' attribute of the tag clicked) which is present as the part of the link i clicked. 当前在单击“是”按钮时,覆盖层关闭,但是我应如何将其重定向到URL(单击的链接的一部分)中出现的URL(单击的标记的“ href”属性)。

First you should save your <a> url in global variable out of the function: 首先,您应将<a>网址保存在函数之外的全局变量中:

 var linkBuffer;

    $('a.external-link').click(function (e) {

        //prevent browser to follow link
        e.preventDefault();

        //save link
        linkBuffer = $(this).attr('href');

        var popupid = "popuprel";

        $('#' + popupid).fadeIn();

        $('body').append('<div id="fade"></div>');
        $('#fade').css({
            'filter': 'alpha(opacity=80)'
        }).fadeIn();

        var popuptopmargin = ($('#' + popupid).height() + 10) / 2;
        var popupleftmargin = ($('#' + popupid).width() + 10) / 2;

        $('#' + popupid).css({
            'margin-top': -popuptopmargin,
            'margin-left': -popupleftmargin
        });
    });

So when you click Yes on your popup, then just change window location: 因此,当您在弹出窗口中单击“ 是”时,只需更改窗口位置即可:

$('.link-continue').click(function () {
    $('#fade , #popuprel').fadeOut();
    document.location.href = linkBuffer;
});

Update: 更新:

Here is demo for you: http://jsfiddle.net/zur4ik/fzn4Z/ 这是给您的演示: http : //jsfiddle.net/zur4ik/fzn4Z/

Please note , that when you'll click yes on confirmation window, JSFiddle will not allow to load different link (you can check this in console, when you click Yes ), but it will work out of JsFiddle. 请注意 ,当您在确认窗口中单击“是”时,JSFiddle将不允许加载其他链接(当您单击“ 是”时,您可以在控制台中检查此链接),但是它将在JsFiddle中起作用。

I would have $('.link-continue') as an anchor element styled like button, which has link to the url proposed to navigate, so clicking on $('.link-continue') take user to new page in same window. 我将$('.link-continue')作为样式类似于按钮的锚元素,该元素具有到建议导航的URL的链接,因此单击$('.link-continue')会将用户带到同一窗口中的新页面。 In case you want to open new window, preventDefault in click listener of $('.link-continue') , and use window.open for same href . 如果要打开新窗口,请在$('.link-continue')单击侦听器中使用preventDefault ,并将window.open用于相同的href

if #popupre1 contains <a class = "link-continue">YES</a> you can try change your Show overlay function like this 如果#popupre1包含<a class = "link-continue">YES</a> ,则可以尝试更改显示叠加功能,如下所示

$('a.external-link').click(function() {

    var popupid = "popuprel";

    $('#' + popupid).fadeIn().find('.link-continue').attr('href',$(this).attr('href'));

    $('body').append('<div id="fade"></div>');
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();

    var popuptopmargin = ($('#' + popupid).height() + 10) / 2;
    var popupleftmargin = ($('#' + popupid).width() + 10) / 2;

    $('#' + popupid).css({
        'margin-top' : -popuptopmargin,
        'margin-left' : -popupleftmargin
    });

    return false;
});

option click function not changed 选项单击功能未更改

Instead of 代替

document.location.href = linkBuffer;

using 运用

window.open(linkBuffer,'_blank');

worked Thanks. 工作谢谢。

So the code for Continue button click becomes: 因此,继续按钮单击的代码变为:

$('.link-continue').click(function () {
$('#fade , #popuprel').fadeOut();
document.location.href = linkBuffer;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM