【问题标题】:replace javascript alerts with modal用模态替换 javascript 警报
【发布时间】:2013-09-15 06:39:27
【问题描述】:

如何用模态框替换 javascript 警报?

根据某些数据库结果通过回显触发 javascript 警报

if response is a
{
     <script type="text/javascript">alert("response is a.");</script>
}
else
{
    <script type="text/javascript">alert("response is not a.");</script>
}

我正在使用引导程序,如果我们可以用引导程序模式替换警报,那就完美了

【问题讨论】:

标签: javascript twitter-bootstrap modal-dialog alerts


【解决方案1】:

冒昧为您创建了一个 jsbin 示例,基于您可以在以下问题中找到的代码:JavaScript: Overriding alert()

http://jsbin.com/UzUDOno/1/edit

请注意,覆盖警报可能不是一个好主意,尤其是因为您会错过常规 javascript 警报的阻止属性。 正如您在我的示例中所看到的,如果您快速连续使用 2 个警报,则最后一个将覆盖之前的警报。

【讨论】:

  • 您的 jsbin 示例显示了对 alert('moo') 的调用,然后是 alert('cow')。 “moo”警报永远不会出现。那可能是因为这仍然是异步的。可能会显示“moo”,但它不会在返回之前等待用户响应并且“cow”会覆盖它。它不是浏览器默认alert()的真正替代品。
  • @LS 我在结尾段落中已经清楚地说明了这一点。这个例子是为了说明他当前的代码是如何工作的。但是,众所周知,阻塞行为是为浏览器保留的,永远无法使用模态弹出窗口来实现。
【解决方案2】:

只是补充@Kippie。

http://jsbin.com/sadesukuli/2/edit?html,output

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery.min.js"></script>
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap-responsive.css" rel="stylesheet" type="text/css" />
<script src="http://getbootstrap.com/2.3.2/assets/js/bootstrap.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <script type="text/javascript">
    (function() {
  var proxied = window.alert;
  window.alert = function() {
    modal = $('<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="myModalLabel">Modal header</h3></div><div class="modal-body"><p>One fine body…</p></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Close</button></div></div>');
    modal.find(".modal-body").text(arguments[0]);
    modal.modal('show');
  };
})();


    alert('moo');
    alert('cow');
  </script>
</body>
</html>

"您的 jsbin 示例显示了对 alert('moo') 的调用,然后是 alert('cow')"

我的示例同时显示了“moo”和“cow”。

【讨论】:

    【解决方案3】:

    更新了@omar-fernando-pessoa 答案以使用引导程序 3.4.1

    https://jsbin.com/monupudode/1/edit?html,output

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://code.jquery.com/jquery.min.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
      <script type="text/javascript">
        (function() {
      var proxied = window.alert;
      window.alert = function() {
        modal = $('<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"><div class="modal-dialog" role="document"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 id="myModalTitle" class="modal-title">Modal title</h4></div><div class="modal-body"><p>One fine body&hellip;</p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div></div></div>');
        modal.find(".modal-body").text(arguments[0]);
        modal.modal('show');
      };
    })();
        
        
        alert('moo');
        alert('cow');
      </script>
    </body>
    </html>
    

    并针对 bootstrap 5.1.3 进行了更新

    https://jsbin.com/mimexomela/1/edit?html,output

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://code.jquery.com/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
      <script type="text/javascript">
        (function() {
      var proxied = window.alert;
      window.alert = function() {
        modaldiv = $('<div id="myModal" class="modal" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h5 id="myModalTitle" class="modal-title">Modal title</h5><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div><div class="modal-body"><p>Modal body text goes here.</p></div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button></div></div></div></div>');
        modaldiv.find(".modal-body").text(arguments[0]);
        modal = new bootstrap.Modal(modaldiv);
        modal.show();
      };
    })();
        
        
        alert('moo');
        alert('cow');
      </script>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      相关资源
      最近更新 更多