擴(kuò)展bootstrap的modal模態(tài)框-動態(tài)添加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();
});
/**關(guān)閉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));
}
//阻止事件默認(rèn)行為
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標(biāo)簽后加上target="dialog",并且提供href外部鏈接地址就可以彈出modal框
以上所述是小編給大家介紹的擴(kuò)展bootstrap的modal模態(tài)框-動態(tài)添加modal框-彈出多個modal框,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 在iframe中使bootstrap的模態(tài)框在父頁面彈出問題
- Bootstrap彈出框之自定義懸??驑?biāo)題、內(nèi)容和樣式示例代碼
- Bootstrap的popover(彈出框)2秒后定時消失的實(shí)現(xiàn)代碼
- Bootstrap實(shí)現(xiàn)提示框和彈出框效果
- Bootstrap實(shí)現(xiàn)帶動畫過渡的彈出框
- Bootstrap編寫一個在當(dāng)前網(wǎng)頁彈出可關(guān)閉的對話框 非彈窗
- Bootstrap彈出框(modal)垂直居中的問題及解決方案詳解
- 關(guān)于Bootstrap彈出框無法調(diào)用問題的解決辦法
- JS組件Bootstrap實(shí)現(xiàn)彈出框和提示框效果代碼
- bootstrap實(shí)現(xiàn)點(diǎn)擊刪除按鈕彈出確認(rèn)框的實(shí)例代碼
相關(guān)文章
JavaScript顯示表單內(nèi)元素數(shù)量的方法
這篇文章主要介紹了JavaScript顯示表單內(nèi)元素數(shù)量的方法,涉及javascript操作表單屬性的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
JavaScript實(shí)現(xiàn)文本相似度對比
這篇文章主要介紹了JavaScript實(shí)現(xiàn)文本相似度對比,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
JavaScript實(shí)現(xiàn)與web通信的方法詳解
這篇文章主要介紹了JavaScript實(shí)現(xiàn)與web通信的方法詳解,文章通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
javascript json字符串到j(luò)son對象轉(zhuǎn)義問題
今天小編就為大家分享一篇關(guān)于javascript json字符串到j(luò)son對象轉(zhuǎn)義問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
每天一篇javascript學(xué)習(xí)小結(jié)(屬性定義方法)
這篇文章主要介紹了javascript中的屬性定義方法知識點(diǎn),對屬性定義方法的基本使用方法,以及各種方法進(jìn)行整理,感興趣的小伙伴們可以參考一下2015-11-11

