简体   繁体   English

在bootstrap 3 modal中加载特定的外部内容

[英]Load specific external content in bootstrap 3 modal

Is it possible to load external content from a specific div into a Bootstrap 3.3.x Modal? 是否可以将来自特定div的外部内容加载到Bootstrap 3.3.x Modal中?

Here's my setup: 这是我的设置:

Page 1 = mywebsite.com/doctors , which will open the modal. 第1页= mywebsite.com/doctors ,它将打开模式。

Page 2 = mywebsite.com/doctors/john-doe , which has a div named #bio . 第2页= mywebsite.com/doctors/john-doe ,其div名为#bio

Can I load the content from #bio into the modal on Page 1? 我可以将#bio的内容#bio到第1页的模式中吗?

I've found info on loading a full external page into the modal, but I really need to load only the content from a specific div. 我已经找到了将整个外部页面加载到模式中的信息,但是我真的只需要加载特定div中的内容。

I pieced together this bit of ajax for loading a specific element, but I'm not sure how to integrate it with the bootstrap modal (or if it's even the best way to do this). 我拼凑了这部分ajax以加载特定的元素,但是我不确定如何将其与bootstrap模态集成(或者是否是执行此操作的最佳方法)。

$.ajax({
    url: 'mywebsite.com/doctors/john-doe',
    dataType: 'html',
    success: function(html) {
        var div = $('#bio', $(html)).addClass('done');
        $('.modal-content').html(div);
    }
});

Thanks for any help. 谢谢你的帮助。

You can use .load() 您可以使用.load()

Loading Page Fragments 加载页面片段

The .load() method, unlike $.get() , allows us to specify a portion of the remote document to be inserted. $.get()不同, .load()方法允许我们指定要插入的远程文档的一部分。 This is achieved with a special syntax for the url parameter. 这可以通过使用url参数的特殊语法来实现。 If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded. 如果字符串中包含一个或多个空格字符,则假定字符串中第一个空格之后的部分是确定要加载内容的jQuery选择器。

We could modify the example above to use only part of the document that is fetched: 我们可以修改上面的示例以仅使用获取的文档的一部分:

$( "#result" ).load( "ajax/test.html #container" );

$(".modal-content").load("mywebsite.com/doctors/john-doe #bio"
, function(html, textStatus, jqxhr) {
  // do stuff when `#bio` element at `mywebsite.com/doctors/john-doe`
  // loaded as `html` of `.modal-content`
});

I think you're in the right path. 我认为您的做法正确。

You'll have to inject this ajax html content received from mywebsite.com/doctors/john-doe into the dialog's modal-body . 您必须将从mywebsite.com/doctors/john-doe接收到的此ajax html内容注入对话框的modal-body

Bootstrap modal dialog html structure: Bootstrap模态对话框 html结构:

<div class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

So, one approach is something like that: (1) open dialog with an loading spinner, (2) trigger this ajax request (3) in the success callback call $('.modal-body').html to set this div content with the received response. 因此,一种方法是这样的:(1)打开带有加载微调器的对话框,(2)在成功回调调用$('.modal-body').html触发此ajax请求(3)设置此div内容与收到的答复。

Partial content 部分内容

One option of parsing received html content is to put your html inside jQuery constructor, like var content = $(received_html); 解析收到的html内容的一种选择是将html放入jQuery构造函数中,例如var content = $(received_html); . Then you cand find your desired div with content.find('#myDiv'); 然后,您可以通过content.find('#myDiv');找到所需的div content.find('#myDiv'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM