简体   繁体   English

Bootstrap模式仅在刷新页面后有效

[英]Bootstrap modal only works after refreshing page

I am experiencing a very strange issue with Bootstrap Modal: 我遇到了Bootstrap Modal的一个非常奇怪的问题:

The first time I load my web page with browser cache cleared and click on an icon that should open 我第一次加载浏览器缓存的网页,然后点击应该打开的图标
the modal it does not open. 它没有打开的模态。 When I refresh the page and click the icon, the modal appears... When I close the modal and click the icon again it works again. 当我刷新页面并单击图标时,模态出现...当我关闭模态并再次单击该图标时,它再次起作用。

This is my header code: 这是我的标题代码:

<link href="libs/jquery/jquery-ui.css" rel="stylesheet" />
<link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" />

<script src="libs/jquery/jquery-2.1.4.min.js"></script>
<script src="libs/jquery/jquery-migrate-1.2.1.min.js"></script>
<script src="libs/jquery/jquery-1.11.3.min.js"></script>
<script>$(document).ready(function() { $.noConflict(); )};</script>
<script src="libs/bootstrap/js/bootstrap.min.js"></script>
<script src="libs/jquery/jquery-ui.js"></script>

This is my body code: 这是我的身体代码:

<a id="myIcon">Open Modal</a>

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Title</h4>
      </div>
      <div class="modal-body">Body</div>
      <div class="modal-footer">Footer</div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

And for those who want to see the full code, this is my JS: 对于那些想要查看完整代码的人来说,这是我的JS:

$(document).ready(function() {
    $('#myIcon').click(function () {
        $('#myModal').modal('show');
    }
});

I know what is happening. 我知道发生了什么。 You are forgetting set some attributes in your anchor. 您忘记在锚中设置一些属性。

<a id="myIcon" data-toggle="modal" data-target="#myModal">Open Modal</a>

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Title</h4>
            </div>
            <div class="modal-body">Body</div>
            <div class="modal-footer">Footer</div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

You could avoid your script. 你可以避免你的脚本。 In this case your script is simply simulating a click on the modal. 在这种情况下,您的脚本只是模拟模态上的单击。 Bootstrap already has an event associated to the buttons which have the attributes that I have wrote in your anchor. Bootstrap已经有一个与按钮关联的事件,这些按钮具有我在锚点中写入的属性。

$(document).ready(function() { 
    // You are triggering a click in your button
    $('#myIcon').click(function () {
        // After the click, you force the modal to be shown
        $('#myModal').modal('show');
    }
});

You can allow only JS interactions with modals. 您只能允许JS与模态交互。 This way, modals will not have an associated event. 这样,模态就不会有相关的事件。

<a class="open_modal" data-modal="myModal">Open Modal</a>

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Title</h4>
            </div>
            <div class="modal-body">Body</div>
            <div class="modal-footer">Footer</div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

You remove the anchor's attributes, and then: 您删除锚的属性,然后:

$(document).ready(function() { 

    $('.open_modal').on('click',function () {
        var idModal = $(this).data("modal")
        $(idModal).modal('show');
    }
});

This way, if a .open_modal element is clicked, its data-modal, that represents a modal id, it's used to show the modal. 这样,如果单击.open_modal元素,其数据模态,表示模态ID,它用于显示模态。

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

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