扩展bootstrap的modal模态框-动态添加modal框-弹出多个modal框

js代码

function initView(_box){ 
  var $p = $(_box || document); 
  $('a[target="dialog"]', $p).each(function(event){ 
    $(this, $p).unbind('click').click(function(event){ 
      openModal(event); 
    }); 
  }); 
} 
$(function(){ 
  initView(); 
}); 
/**关闭modal*/ 
function hideModal(obj){ 
  var modal = $(obj).parents("div.modal"); 
  if(modal.length > 0){ 
    modal.remove(); 
    initView(); 
  } 
} 
/**打开modal*/ 
function openModal(event){ 
// var this = $(this); 
  var $this = $(event.currentTarget); 
  var _url = $this.attr("href"); 
  var _title = $this.attr("title"); 
  var _id; 
  _id = dialog.content(); 
  var options = { 
      backdrop: false, 
      keyboard: true, 
      show: true 
  }; 
  $('#' + _id).modal(options); 
  var modal = $('#' + _id); 
  if(typeof(_title) != "undefined"){ 
     if(modal.find('.modal-title').length <= 0){ 
       var header = dialog.header({title : _title}); 
       $($.parseHTML(header)).appendTo(modal.find(".modal-content")); 
     }else{ 
       modal.find('.modal-title').text(_title); 
     } 
     if(modal.find('.modal-body').length <= 0){ 
       var _body = dialog.body; 
       $($.parseHTML(_body)).appendTo(modal.find(".modal-content")); 
     } 
     modal.find(".modal-body").load(_url, $.proxy(function () { 
       modal.trigger('loaded.bs.modal'); 
       initView(); 
      }, this)); 
   }else{ 
     modal.find(".modal-content").load(_url, $.proxy(function () { 
       modal.trigger('loaded.bs.modal'); 
       initView(); 
      }, this)); 
   } 
    //阻止事件默认行为 
  event.preventDefault(); 
} 
//modal model 
//TO STRAT 
if(!$(window).data("_modal_id")){ 
  $(window).data("_modal_id", 0); 
} 
var dialog = { 
  header : function(options){ 
    var template = '<div class="modal-header">' 
            +  '<button type="button" class="close" aria-label="Close" onclick="hideModal(this);"><span aria-hidden="true">×</span></button>' 
            +  '<h4 class="modal-title">' + options.title + '</h4>' 
            +'</div>'; 
    return template; 
  }, 
  content : function(){ 
    var _modal_id = $(window).data("_modal_id"); 
    var _id = "_modal_id_" + _modal_id; 
    _modal_id ++; 
    $(window).data("_modal_id", _modal_id); 
    var template = '<div class="modal fade" tabindex="-1" role="dialog" id="'+ _id +'">' 
     +  '<div class="modal-dialog modal-lg" role="document" aria-hidden="true">' 
     +   '<div class="modal-content">' 
     +   '</div>' 
     +  '</div>' 
     +'</div>'; 
    $(template).appendTo('body'); 
    initView(); 
    return _id; 
  }, 
  body : '<div class="modal-body"></div>' 
}; 
//TO END 

页面代码:

<a href="select.html" rel="external nofollow" id="signId" class="btn btn-info" <span style="background-color: rgb(255, 255, 102);">target="dialog"</span> title="请选择用餐类型">签到</a> 

页面上只要在a标签后加上target="dialog",并且提供href外部链接地址就可以弹出modal框

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现多个弹出,可以通过动态创建多个模态框的方式来实现。以下是一种实现方法: 1. 在 HTML 中添加一个按钮,用于触发弹出: ```html <button id="btnOpenModal" class="btn btn-primary">打开模态框</button> ``` 2. 在 JavaScript 中编写代码,用于动态创建模态框: ```javascript // 模态框计数器,用于生成唯一的 ID var modalCount = 0; // 创建模态框函数 function createModal() { // 创建模态框 HTML var modalHTML = '<div id="modal' + modalCount + '" class="modal fade" tabindex="-1" role="dialog">'; modalHTML += '<div class="modal-dialog" role="document">'; modalHTML += '<div class="modal-content">'; modalHTML += '<div class="modal-header">'; modalHTML += '<h5 class="modal-title">模态框标题</h5>'; modalHTML += '<button type="button" class="close" data-dismiss="modal" aria-label="Close">'; modalHTML += '<span aria-hidden="true">×</span>'; modalHTML += '</button>'; modalHTML += '</div>'; modalHTML += '<div class="modal-body">'; modalHTML += '<p>模态框内容</p>'; modalHTML += '</div>'; modalHTML += '<div class="modal-footer">'; modalHTML += '<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>'; modalHTML += '<button type="button" class="btn btn-primary">保存</button>'; modalHTML += '</div>'; modalHTML += '</div>'; modalHTML += '</div>'; modalHTML += '</div>'; // 将模态框 HTML 添加到页面中 $('body').append(modalHTML); // 增加计数器,确保每个模态框具有唯一的 ID modalCount++; // 返回最新创建的模态框的 ID return 'modal' + (modalCount - 1); } ``` 3. 在 JavaScript 中编写代码,用于打开模态框: ```javascript // 点击按钮时打开模态框 $('#btnOpenModal').click(function () { // 创建一个新的模态框 var modalId = createModal(); // 打开新的模态框 $('#' + modalId).modal('show'); }); ``` 这样就可以通过点击按钮来打开多个模态框了。每次点击按钮时,都会动态创建一个新的模态框,并打开它。注意,这里使用了 jQuery 和 BootstrapJavaScript 插件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ok060

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值