为了账号安全,请及时绑定邮箱和手机立即绑定

当 jQuery 对话框打开时,我不希望用户与背景交互

当 jQuery 对话框打开时,我不希望用户与背景交互

慕雪6442864 2021-11-25 16:48:15
如果用户单击外部链接,jQuery UI 对话框会显示“确定”和“取消”按钮,当他们点击“确定”时,它应该将它们带到外部站点。一切都按预期工作,但问题在于modal:true;当对话框打开时,用户可以在背景上工作,我希望在弹出窗口打开时背景变灰。$(window).on('load', function(){    $.expr[":"].external = function(a) {        var linkhn = a.hostname.split('.').reverse();        var linkHref = linkhn[1] + "." + linkhn[0];        var domainhn = window.location.hostname.split('.').reverse();        var domainHref = domainhn[1] + "." + domainhn[0];        return !a.href.match(/^mailto\:/) && !a.href.match(/^tel\:/) && linkHref !== domainHref;    };    $("a:external").addClass("ext_link");});$(document).ready(function(){    jQuery(document).on("click", ".ext_link", function () {$("<div>Thank you for visiting You are now being taken to an external site. </div>").dialog().attr('id','dialog-confirm').css('display','none');var link_val = this;  $('#dialog-confirm').dialog({   title: "External Link",      height: "auto",      width: 410,    resizable: false,      modal: false,    buttons: {      "Ok": function() {         window.open( link_val.href); $(this).dialog("close"); $(this).dialog('destroy').remove();      },      Cancel: function () {jQuery(this).dialog("close"); $(this).dialog('destroy').remove() ;}    }  }).dialog("widget").find(".ui-dialog-titlebar").hide();   return false;});});
查看完整描述

1 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

您可以使用另一个打开并覆盖整个页面的 div,下面的代码就是这样编写的。


不会打扰您的大部分代码,我添加<div>了将在页面加载时隐藏的代码。这<div>将在弹出窗口打开时显示,并<div>在对话框关闭时隐藏。


$(document).ready(function() {

  jQuery(document).on("click", ".ext_link", function() {

    $('#scrLoader').show();

    $("<div>Thank you for visiting You are now being taken to an external site. </div>").dialog().attr('id', 'dialog-confirm').css('display', 'none');

    var link_val = this;

    $('#dialog-confirm').dialog({

      title: "External Link",

      height: "auto",

      width: 410,

      resizable: false,

      buttons: {

        "Ok": function() {

          window.open(link_val.href);

          $(this).dialog("close");

          $(this).dialog('destroy').remove();

        },

        Cancel: function() {

          $('#scrLoader').hide();

          jQuery(this).dialog("close");

          $(this).dialog('destroy').remove();

        }

      }

    }).dialog("widget").find(".ui-dialog-titlebar").hide();

    return false;

  });

});

#scrLoader {

  background-color: #333;

  opacity: 0.8;

  position: fixed;

  left: 0px;

  top: 0px;

  z-index: 100;

  height: 100%;

  width: 100%;

  overflow: hidden;

  background-image: url('your path to a loader gif');

  background-position: center;

  background-repeat: no-repeat;

}

<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>


<script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>


<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" />

<input type="button" class="ext_link" value="click me" />

<div id="scrLoader" style="display: none;" hidden="true"></div>


您还可以使用 GIF,例如加载程序 GIF,它会在特定 div 打开时显示,以便用户了解某些进程正在进行中。


希望这可以帮助。


查看完整回答
反对 回复 2021-11-25
  • 1 回答
  • 0 关注
  • 159 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信