jQuery Bootstrap 模式不会出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20053266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap modal won't show up
提问by Magnus Burton
My bootstrap (3.0.2) modal won't show up. I've tried different buttons, links and all that but it simply won't show up. I appreciate any help!
我的引导程序 (3.0.2) 模式不会出现。我尝试了不同的按钮、链接和所有这些,但它根本不显示。我感谢任何帮助!
<head>
<link rel="stylesheet" href="css/bootstrap.min.css" />
</head>
<body>
<button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#purchaseModal">Purchase</button>
<div class="modal fade" id="purchaseModal" tabindex="-1" role="dialog" aria-labelledby="purchaseLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="purchaseLabel">Purchase</h4>
</div>
<div class="modal-body">
sup?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Purchase</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="js/jquery-ui-1.10.3.custom.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
</body>
UPDATE 1:Here's a screenshot of the warnings in the console.
更新 1:这是控制台中警告的屏幕截图。
UPDATE 2:The button and the modal are located inside a container. Maybe this could help on your path to a solution?
更新 2:按钮和模态位于容器内。也许这可以帮助您找到解决方案?
回答by sabotero
This code is working fine. You have an javaScript error somewhere:
这段代码工作正常。你在某处有一个 javaScript 错误:
Working code:
工作代码:
<!DOCTYPE html >
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<!-- Bootstrap -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
</head>
<body>
<button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#purchaseModal">Purchase</button>
<div class="modal fade" id="purchaseModal" tabindex="-1" role="dialog" aria-labelledby="purchaseLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="purchaseLabel">Purchase</h4>
</div>
<div class="modal-body">
sup?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Purchase</button>
</div>
</div>
</div>
</div>
</body>
</html>
回答by Magnus Burton
I solved this by adding this line of JQuery to the bottom of the page;
我通过在页面底部添加这行 JQuery 来解决这个问题;
<script>
$("[data-toggle='modal']").modal();
</script>
回答by Dimitri Lilov
I think you need to put jquery and bootstraps javascript files path in the head of you html. I was having the same problem and this worked for me.
我认为您需要将 jquery 和 bootstraps javascript 文件路径放在 html 的头部。我遇到了同样的问题,这对我有用。
<head>
<link rel="stylesheet" href="plugins/bootstrap/css/bootstrap.css">
<!-- put first the jquery path, otherwise the bootstrap.js won't work-->
<script src="js/jquery/jquery-3.1.0.js"></script>
<script src="plugins/bootstrap/js/bootstrap.min.js"></script>
</head>
Hope it worked :)
希望它有效:)
回答by Lucky
I have the same problem. when i add this code :
我也有同样的问题。当我添加此代码时:
let postBody = event.target.parentNode.parentNode.childNodes[1].textContent;
modal will not show up . and when i remove it the modal appears.
模态不会出现。当我删除它时,会出现模态。
回答by Chris Turner
Adding this one line of code to the head solved the issue for me.
将这一行代码添加到头部为我解决了这个问题。
= javascript_include_tag 'application', 'data-turbolinks-track' => true