当前位置: 代码迷 >> JavaScript >> 在iframe上打开Bootstrap Modal窗口
  详细解决方案

在iframe上打开Bootstrap Modal窗口

热度:12   发布时间:2023-06-12 13:48:42.0

我想打开一个iframe的模式对话框。 当用户单击时,按钮应加载URL并以模式形式弹出URL内容。

但是,由于某些原因,此操作将失败,如下面的代码所示。 我是从借用这个想法的

该示例使用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">&times;</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> 

  相关解决方案