本文介绍了模态打开时,将类添加到主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的登录模式有问题。一个模式打开时,有人点击忘记密码链接在我登录模式。 (一种模式打开另一种模式)所以我通过添加所有必要的属性来清除所有事物。
例如:

 < span data-toggle =modaldata-target =#forgot-pin- popupdata-dismiss =modal>忘记密码?< / span> 

但我注意到它不会将 .modal-open 类添加到当我的第二个模态被打开时(没有这个类,样式显示很奇怪)。所以我手动使用这个js脚本。

  $('。modal')。on('hidden.bs.modal',函数(){
if($('。modal')。hasClass('in')){
$('body')。addClass('modal-open');
}
});

当我尝试切换模型6次时,完美地工作。它不会再工作。无法想象为什么。我无法向您展示一个示例,因为它位于我的制作网站上。有没有人会遇到类似的问题? .modal

  $('body')。on('hidden.bs.modal',如果($('。modal.in')。)($ body)。 $ b}); 

还要确保你的模态不应该嵌套到 .modal class



$('body')。on 'hidden.bs.modal',function(){if($('。modal.in')。length){$('body')。addClass('modal-open');}}); $( (click,function(){console.log($(body)。attr(class));}) < script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1。

I'm having a problem with my login modals. One modal open when someone click forgot password link on my sign in the modal. (One modal open in another modal) So I came everything clean by adding all necessary attributes.Eg:

  <span data-toggle="modal" data-target="#forgot-pin-popup" data-dismiss="modal">Forgot password?</span>

But I notice it won't add .modal-open class to the body when my second modal is open(styles are shown weird without this class). So I manually use this js script.

  $('.modal').on('hidden.bs.modal', function () {
  if($('.modal').hasClass('in')) {
   $('body').addClass('modal-open');
   }
 });

Works perfectly BUT when I try switching models like 6 times. It won't work again. Can't imagine why. I can't show you an example because it's on my production site. Does anyone face the similar problem like this?

解决方案

Try to use body instead of .modal

$('body').on('hidden.bs.modal', function() {
  if ($('.modal.in').length) {
    $('body').addClass('modal-open');
  }
});

Also make sure that your modal should not be nested into .modal class

$('body').on('hidden.bs.modal', function() {
  if ($('.modal.in').length) {
    $('body').addClass('modal-open');
  }
});
$(".btn-primary").on("click", function() {
  console.log($("body").attr("class"));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Open modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <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 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1" data-dismiss="modal">
  Open modal
</button>
      </div>
    </div>
  </div>
</div>

<!-- Modal1 -->
<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <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 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

这篇关于模态打开时,将类添加到主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:56