javascript - 根据内容使用动态宽度的 Bootstrap 弹出窗口

标签 javascript jquery html css twitter-bootstrap

我已经创建了 Bootstrap 模式,并通过 AJAX 附加了一些 html...

我无法根据内容显示完美的宽度。我试过:

width: 'auto'; //it looks as in Screenshot
width: 100%;   //it takes whole page width

看起来像:

image1

var PopupHelper = {
  Show: function () {
    $("#popup_content").empty();
    var qAjax = new AjaxHelper("/Services/Service.asmx/GetCurrentPopupContent");
    qAjax.OnSuccess = function (data) {
      $("#popup_content").html(data);
      $('#popup-Model').css({ 'width' : 'auto','overflow': 'auto' });
      $("#popup-Model").modal();
    }
    qAjax.Init();
  }
}
<!--Popup Window -->
<div class="modal fade" id="popup-Model" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-top:-10px !important">&times;</button>
      </div>
      <div class="modal-body" id="popup_content">
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!--Popup Window -->

如何根据 modal-body 中的内容设置模态宽度?

NOTE: modal HTML shown in image can vary

谢谢!!!

最佳答案

由于您已经在使用 javascript 来呈现此模式,因此将 modal-dialog 宽度设置为与您的内容相同的宽度会更容易。仅使用 CSS 很难做到这一点,因为 modal-dialog 具有固定宽度(因屏幕尺寸而异)。

$("#popup_content").html(data);
$("#popup-Model").modal();
setTimeout(() => {     
  //change div:first-child to whatever your first child is 
  var width = $("#popup_content div:first-child").width() + 30; //Padding
  $(".modal .modal-dialog").css({ 'width' : width+'px' });
}, 0); //Maybe you'll need a longer timeout because of css transitions

working example

在 OP 案例中担任:

$("#popup_content").empty();
var qAjax = new AjaxHelper("/Services/Service.asmx/GetCurrentPopupContent");
qAjax.OnSuccess = function (data) {
$("#popup_content").html('<div class="test-div">' + data + '</div>');
$("#popup-Model").modal();
setTimeout(function () {
    //change div:first-child to whatever your first child is 
    var width = $("#popup_content div:first-child").width() + 50; //padding
    $(".modal-dialog").css({ 'width': width + 'px' });
    $('#popup-Model').css({ 'overflow': 'auto' });
}, 500);

关于javascript - 根据内容使用动态宽度的 Bootstrap 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40992615/

相关文章:

javascript - 如何创建一个包含帖子和每个帖子回复的论坛

javascript - 在 JQuery 中动态创建闪存卡?

javascript - 使用 native react 查看列表中的项目

javascript - bootstrap的开关+AJAX

javascript - 在 Jade 中内联 Javascript,同时保持缩进

javascript - 我怎样才能扩展这个?

javascript - 如何使用箭头键将文本区域从一个 div 聚焦到另一个 div

javascript - 如何在javascript中的setAttribute()方法中将参数添加到函数中

javascript - 在 session 中保存对象数组

javascript - 将 <div> 设置为比动态大小的 <div> 高特定数量