简体   繁体   English

Bootstrap模态数据不起作用

[英]Bootstrap modal data dismiss not working

So, the the data-dismiss="modal" is not working for buttons that are added in my HTML, but is working for buttons that are added to the modal via JS inserting the button HTML. 因此, data-dismiss="modal"不适用于我的HTML中添加的按钮,但适用于通过JS插入按钮HTML添加到模态的按钮。 This makes me figure that there's something wrong with where/what order I am adding my scripting tags. 这让我觉得我添加脚本标签的位置/顺序有问题。

My scripting tags are in this order: 我的脚本标签按此顺序排列:

<script src="/Scripts/jquery-2.1.1.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<script src="/Scripts/Shared/site.js"></script>

I tried putting them in the <head> . 我试着把它们放在<head> I moved them to be the last thing right before </body> . 我把它们移到了</body>之前的最后一件事。 I moved them to be right before and right after the modal html. 我把它们移到模态html之前和之后。

UPDATE UPDATE

Here's a simplified version of my code: 这是我的代码的简化版本:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
    <link href="/Content/bootstrap.css" rel="stylesheet"/>
    <link href="/Content/font-awesome.css" rel="stylesheet"/>
    <link href="/Content/site-bootstrap.css" rel="stylesheet"/>
    <link href="/Content/site.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.8.3.js"></script>
</head>
<body>
    <div id="Popup" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div id="PopupHeader">
                    <button type="button" id="PopupX" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 id="PopupTitle" class="modal-title floatLeft"></h4>
                    <div class="clear"></div>
                </div>
                <div class="modal-body bgColorWhite">
                    <div id="PopupContent">Test</div>
                </div>
                <div id="PopupButtons" class="modal-footer bgColorWhite">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <script src="/Scripts/jquery-2.1.1.js"></script>
    <script src="/Scripts/bootstrap.js"></script>
    <script src="/Scripts/respond.js"></script>
    <script src="/Scripts/Shared/site.js"></script>
</body>
</html>

Another note: 另一个说明:

If I add the "Close" button, aka the X, via JS, it works as well. 如果我通过JS添加“关闭”按钮,也就是X,它也可以。 It's having it already in the modal that causes issues. 它已经在模式中导致了问题。

Any suggestions? 有什么建议?

I was able to figure out my issue. 我弄清楚了我的问题。 It was that in my own JS file, I wanted to add into every button click event, event.stopPropagation(); 就是在我自己的JS文件中,我想添加到每个按钮点击事件,event.stopPropagation(); Since this was a method, and I did not want this added multiple times to the same button every time it was called, I would remove the previous click events, which was removing Bootstrap's dismiss click event. 由于这是一个方法,并且我不希望每次调用它时多次添加到同一个按钮,我会删除之前的单击事件,这将删除Bootstrap的dismiss click事件。

So, if you have this same issue and you have all of Bootstrap's JS added correctly, check your own code to make sure you're not overwriting/removing events. 所以,如果你有同样的问题并且你已经正确添加了Bootstrap的所有JS,请检查你自己的代码以确保你没有覆盖/删除事件。

UPDATE UPDATE

Just to clarify, event.stopPropagation() prevents data-dismiss from working. 只是为了澄清,event.stopPropagation()可以防止数据解除工作。

Try put into 'modal-content' div: 尝试放入'模态内容'div:

<div class="modal-header">...</div>
<div class="modal-body">
  ...
  your content
  ...
</div>
<div class="modal-footer">
  ... 
  your buttons
  ...
</div>

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

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