如何使用显示变量url的iframe打开bootstrap模式

时间:2016-01-02 23:36:24

标签: javascript html twitter-bootstrap iframe bootstrap-modal

我有一个正常的大型引导模式:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel>Google.com</h4>
      </div>
      <div class="modal-body">
      <iframe src="https://google.com" width="90%" height="90%" frameborder="0" allowtransparency="true"></iframe>
      </div>
      <div class="modal-footer">
      <a type="button" class="btn btn-primary"  href="https://google.com" target="_blank">Go to actual page</a>
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>

包含一个iframe。我想创建一个打开上述模态的函数,但我希望函数能够更改iframe的src(它是https://google.com),以及链接到实际页面的按钮,作为函数的url参数,以及标题(表示google.com),是函数的title参数中的任何内容。我真的不知道怎么开始这个。最好的方法是使用document.write(),然后将信息存储在cookie中吗?或其他一些方式。

function openModal(url, title) {
  //code
}

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
function openModal(url, title) {
  $('#myModalLabel').text(title);
  $('.modal-body iframe').attr('src', url);
  $('#myModal').modal('show');
}

$('button').click(function() {
  openModal('http://www.bing.com/', 'bing.com');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Google.com</h4>
      </div>
      <div class="modal-body">
        <iframe src="https://google.com" width="90%" height="90%" frameborder="0" allowtransparency="true"></iframe>
      </div>
      <div class="modal-footer">
      <a type="button" class="btn btn-primary"  href="https://google.com" target="_blank">Go to actual page</a>
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>

<button>Open Modal</button>
&#13;
&#13;
&#13;