Bootstrap模态恢复按钮焦点关闭

时间:2015-05-19 10:22:43

标签: css twitter-bootstrap

我很难与一种效果斗争,我相信在Bootstrap中需要,但我想逃避。我有按钮:

<a data-toggle="modal" data-target="#video" class="btn btn-primary btn-lg" href="#">Video<i class="pe-7s-angle-right pe-2x pe-va" style="line-height: 0.3;"></i></a>

用一些视频打开模态窗口:

<div class="modal fade video-lightbox in" id="video" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" style="display: block;"><div class="modal-backdrop fade in" style="height: 442px;"></div>

因此,当我关闭模态时,我的按钮会自动聚焦。这个效果是否需要在Bootstrap中,我该如何逃避呢?关闭模态后,我不希望我的按钮处于焦点。

5 个答案:

答案 0 :(得分:2)

您只需在custom.js文件中编写一个函数即可。

$(document).ready(function() {
    $('.close')on("click", function() {
        $(".btn").blur();
    });
});

答案 1 :(得分:2)

模态关闭时模糊按钮。

$('body').on('hidden.bs.modal', '.modal', function() {
    $('.btn').blur();
}); 

答案 2 :(得分:1)

通过删除

解决了这个问题
d.trigger("focus")    

在bootstrap.min.js中:

a.fn.modal = b, a.fn.modal.Constructor = c, a.fn.modal.noConflict = function() {
    return a.fn.modal = d, this
}, a(document).on("click.bs.modal.data-api", '[data-toggle="modal"]', function(c) {
    var d = a(this),
        e = d.attr("href"),
        f = a(d.attr("data-target") || e && e.replace(/.*(?=#[^\s]+$)/, "")),
        g = f.data("bs.modal") ? "toggle" : a.extend({
            remote: !/#/.test(e) && e
        }, f.data(), d.data());
    d.is("a") && c.preventDefault(), f.one("show.bs.modal", function(a) {
        a.isDefaultPrevented() || f.one("hidden.bs.modal", function() {
            d.is(":visible") && d.trigger("focus")
        })
    }), b.call(f, g, this)
})

答案 3 :(得分:1)

默认情况下,Bootstrap链接或按钮在关闭模式后获得焦点。您可以使用以下代码转义它:

$('#mymodal').on('hidden.bs.modal', function () {
       relatedTarget.blur();
   }); 

答案 4 :(得分:0)

您可以使用此方法在关闭模态时使模态触发器失去焦点:

$('#myModal').on('shown.bs.modal', function (e) {
    $('#modalTrigger').one('focus', function (e) {
        $(this).blur();
    });
});

示例:

$(document).ready(function() {
    $('#myModal').on('shown.bs.modal', function (e) {
        $('#modalTrigger').one('focus', function (e) {
            $(this).blur();
        });
    });
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</head>
<body>
  <button id="modalTrigger" type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Open modal
  </button>

  <!-- The Modal -->
  <div class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">
      
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

相关问题