javascript - 在 iframe 上打开 Bootstrap 模态窗口
问题描述
我想打开一个 iframe 的模态对话框。当用户单击按钮时,应加载 URL 并将 URL 内容作为 Modal 弹出。
但是,由于某些原因,这会失败,如下面的代码所示。我从这个http://jsfiddle.net/limeric29/C3LkL/借用这个想法
该示例使用 JQuery 1.8,但我想使用 3.3。我怀疑这就是失败的原因。
$('a.btn').on('click', function(e) {
e.preventDefault();
var url = $(this).attr('href');
$(".modal-body").html('<iframe width="100%" height="100%" frameborder="0" scrolling="no" allowtransparency="true" src="'+url+'"></iframe>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.min.js"></script>
<a data-toggle="modal" class="btn" href="http://www.bing.com" data-target="#myModal">click me</a>
<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
</div>
</div>
有人带领我吗?
解决方案
试试这个
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<a id="zoe" href="https://www.bing.com" data-toggle="modal" data-target="#myModal">Click Me</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<script>
$('#zoe').on('click', function(e) {
e.preventDefault();
var url = $(this).attr('href');
$(".modal-body").html('<iframe width="100%" height="100%" frameborder="0" scrolling="no" allowtransparency="true" src="'+url+'"></iframe>');
});
</script>
</body>
</html>
推荐阅读
- c# - 如何等待 c# webbrowser 在 TPL 中完成文档加载
- python - 以下程序的 Big-O 符号是否正确?
- http - 使用从请求中捕获的变量作为 IntelliJ HTTP 客户端中下一个请求的路径的一部分
- mysql - InnoDB 无法为缓冲池分配内存
- arrays - 返回具有数组字段的文档,该数组字段包含 Elasticsearch 6.x 中用户数组中的所有元素
- python - 如何将builtins.input 用于多个输入
- r - 如何使用 R 中的 oauth2 令牌从 Web 服务器检索数据?
- sql-server - 保存在数据库服务器中的 SSIS 包无法从应用服务器执行
- angular - 如何在使用 Cloudinary 自己的小部件上传器上传之前进行裁剪?
- oracle-sqldeveloper - 如何将此查询放入 SQL Developer 中的存储过程中?