我是靠谱客的博主 激动冬瓜,这篇文章主要介绍在iframe中使bootstrap的模态框在父页面弹出问题,现在分享给大家,希望可以做个参考。

这几天在写项目的中,页面使用了iframe,然而在子页面中使用bootstrap的模态框时发现:弹出的模态框位置是以子页面为标准居中的,并且遮罩层也只有子页面那一部分,整个页面简直无法直视,所以思考可以可以让模态框以父页面为标准弹出,经过在网上查找资料,总结了一种解决方法。

效果展示

修改前页面

这里写图片描述

修改后页面

这里写图片描述

实现思路

要想使模态框在顶级页面打开,我想到的方法是使bootstrap的模态框弹在父页面弹出即可。

首先我们将需要弹出的div单独写一个页面,当子页面需要弹出时,在父页面弹出div即可。

将模态框单独写为一个页面

主页面

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <div class="modal fade" id="ajax" role="basic" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> </div> </div> </div> </body> <script> $("#ajax").modal({ remote: "./model.html" }); </script> </html>

模态框页面

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</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">保存</button> <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> </div> </div>

这样就可以将模态框的div单独写为一个页面

修改主页面,加入子页面

修改后主页面

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <table width="100%" height="720px" border="1"> <tr> <td> <iframe id="iframe1" name="iframe1" src="iframe1.html" width="100%" height="100%"></iframe> </td> <td></td> </tr> <tr> <td></td> <td> <iframe name="iframe2" src="iframe2.html" width="100%" height="100%"></iframe> </td> </tr> </table> <input type="text" id="textId" value="234" /> <div class="modal fade" id="ajax" role="basic" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> </div> </div> </div> </body> <script> function modalOut(url) { $("#ajax").modal({ remote: url; }); } </script> </html>

这样主页面就有2个子页面,并且将弹出模态框的方法封装至modalOut(url)方法中

子页面弹出模态框

当子页面需要弹出模态框时,加载父页面中的modalOut(url)方法即可:

子页面js如下

复制代码
1
2
var _iframe = window.parent; _iframe.modalOut('xxx.html');

以上就是解决子页面中弹出模态框无法覆盖全部页面的解决办法,

注意:所以html中未加载js,请自行加载bootstrap相关的js。

总结

以上所述是小编给大家介绍的在iframe中使bootstrap的模态框在父页面弹出问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

最后

以上就是激动冬瓜最近收集整理的关于在iframe中使bootstrap的模态框在父页面弹出问题的全部内容,更多相关在iframe中使bootstrap内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(156)

评论列表共有 0 条评论

立即
投稿
返回
顶部