Bootstrap實現彈出框和提示框效果代碼

来源:http://www.cnblogs.com/hofmann/archive/2017/01/11/6272513.html
-Advertisement-
Play Games

一、Bootstrap彈出框使用過JQuery UI應該知道,它裡面有一個dialog的彈出框組件,功能也很豐富。與jQuery UI的dialog類似,Bootstrap裡面也內置了彈出框組件。打開bootstrap 文檔可以看到它的dialog是直接嵌入到bootstrap.js和bootstr ...


一、Bootstrap彈出框
使用過JQuery UI應該知道,它裡面有一個dialog的彈出框組件,功能也很豐富。與jQuery UI的dialog類似,Bootstrap裡面也內置了彈出框組件。打開bootstrap 文檔可以看到它的dialog是直接嵌入到bootstrap.js和bootstrap.css裡面的,也就是說,只要我們引入了bootstrap的文件,就可以直接使用它的dialog組件,是不是很方便。本篇我們就結合新增編輯的功能來介紹下bootstrap dialog的使用。廢話不多說,直接看來它如何使用吧。
1、cshtml界面代碼

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">   <div class="modal-dialog" role="document">    <div class="modal-content">     <div class="modal-header">      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>      <h4 class="modal-title" id="myModalLabel">新增</h4>     </div>     <div class="modal-body">        <div class="form-group">       <label for="txt_departmentname">部門名稱</label>       <input type="text" name="txt_departmentname" class="form-control" id="txt_departmentname" placeholder="部門名稱">      </div>      <div class="form-group">       <label for="txt_parentdepartment">上級部門</label>       <input type="text" name="txt_parentdepartment" class="form-control" id="txt_parentdepartment" placeholder="上級部門">      </div>      <div class="form-group">       <label for="txt_departmentlevel">部門級別</label>       <input type="text" name="txt_departmentlevel" class="form-control" id="txt_departmentlevel" placeholder="部門級別">      </div>      <div class="form-group">       <label for="txt_statu">描述</label>       <input type="text" name="txt_statu" class="form-control" id="txt_statu" placeholder="狀態">      </div>     </div>     <div class="modal-footer">      <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>關閉</button>      <button type="button" id="btn_submit" class="btn btn-primary" data-dismiss="modal"><span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存</button>     </div>    </div>   </div>  </div>

最外面的div定義了dialog的隱藏。我們重點來看看第二層的div

?
1 <div class="modal-dialog" role="document">

這個div定義了dialog,對應的class有三種尺寸的彈出框,如下:

?
1 2 3 <div class="modal-dialog" role="document"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-dialog modal-full" role="document">

第一種表示預設類型的彈出框;第二種表示增大的彈出框;第三種表示滿屏的彈出框。role="document"表示彈出框的對象的當前的document。

2、js裡面將dialog show出來。
預設情況下,我們的彈出框是隱藏的,只有在用戶點擊某個操作的時候才會show出來。來看看js裡面是如何處理的吧:

?
1 2 3 4 5 //註冊新增按鈕的事件  $("#btn_add").click(function () {   $("#myModalLabel").text("新增");   $('#myModal').modal();  });

對,你沒有看錯,只需要這一句就能show出這個dialog.

?
1 $('#myModal').modal();

3、效果展示
新增效果

編輯效果

4、說明
彈出框顯示後,點擊界面上其他地方以及按Esc鍵都能隱藏彈出框,這樣使得用戶的操作更加友好。關於dialog裡面關閉和保存按鈕的事件的初始化在項目裡面一般是封裝過的,這個我們待會來看。

二、確認取消提示框
這種類型的提示框一般用於某些需要用戶確定才能進行的操作,比較常見的如:刪除操作、提交訂單操作等。

1、使用bootstrap彈出框確認取消提示框
介紹這個組件之前,就得說說組件封裝了,我們知道,像彈出框、確認取消提示框、信息提示框這些東西項目裡面肯定是多處都要調用的,所以我們肯定是要封裝組件的。下麵就來看看我們封裝的缺乏取消提示框。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 (function ($) {    window.Ewin = function () {   var html = '<div id="[Id]" class="modal fade" role="dialog" aria-labelledby="modalLabel">' +         '<div class="modal-dialog modal-sm">' +          '<div class="modal-content">' +           '<div class="modal-header">' +            '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' +            '<h4 class="modal-title" id="modalLabel">[Title]</h4>' +           '</div>' +           '<div class="modal-body">' +           '<p>[Message]</p>' +           '</div>' +           '<div class="modal-footer">' +   '<button type="button" class="btn btn-default cancel" data-dismiss="modal">[BtnCancel]</button>' +   '<button type="button" class="btn btn-primary ok" data-dismiss="modal">[BtnOk]</button>' +  '</div>' +          '</div>' +         '</div>' +        '</div>';       var dialogdHtml = '<div id="[Id]" class="modal fade" role="dialog" aria-labelledby="modalLabel">' +         '<div class="modal-dialog">' +          '<div class="modal-content">' +           '<div class="modal-header">' +            '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' +            '<h4 class="modal-title" id="modalLabel">[Title]</h4>' +           '</div>' +           '<div class="modal-body">' +           '</div>' +          '</div>' +         '</div>' +        '</div>';   var reg = new RegExp("\\[([^\\[\\]]*?)\\]", 'igm');   var generateId = function () {    var date = new Date();    return 'mdl' + date.valueOf();   }   var init = function (options) {    options = $.extend({}, {     title: "操作提示",     message: "提示內容",     btnok: "確定",     btncl: "取消",     width: 200,     auto: false    }, options || {});    var modalId = generateId();    var content = html.replace(reg, function (node, key) {     return {      Id: modalId,      Title: options.title,      Message: options.message,      BtnOk: options.btnok,      BtnCancel: options.btncl     }[key];    });    $('body').append(content);    $('#' + modalId).modal({     width: options.width,     backdrop: 'static'    });    $('#' + modalId).on('hide.bs.modal', function (e) {     $('body').find('#' + modalId).remove();    });    return modalId;   }     return {    alert: function (options) {     if (typeof options == 'string') {      options = {       message: options      };     }     var id = init(options);     var modal = $('#' + id);     modal.find('.ok').removeClass('btn-success').addClass('btn-primary');     modal.find('.cancel').hide();       return {      id: id,      on: function (callback) {       if (callback && callback instanceof Function) {        modal.find('.ok').click(function () { callback(true); });       }      },      hide: function (callback) {       if (callback && callback instanceof Function) {        modal.on('hide.bs.modal', function (e) {         callback(e);        });       }      }     };    },    confirm: function (options) {     var id = init(options);     var modal = $('#' + id);     modal.find('.ok').removeClass('btn-primary').addClass('btn-success');     modal.find('.cancel').show();     return {      id: id,      on: function (callback) {       if (callback && callback instanceof Function) {        modal.find('.ok').click(function () { callback(true); });        modal.find('.cancel').click(function () { callback(false); });       }      },      hide: function (callback) {       if (callback && callback instanceof Function) {        modal.on('hide.bs.modal', function (e) {         callback(e);        });       }      }     };    },    dialog: function (options) {     options = $.extend({}, {      title: 'title',      url: '',      width: 800,      height: 550,      onReady: function () { },      onShown: function (e) { }     }, options || {});     var modalId = generateId();       var content = dialogdHtml.replace(reg, function (node, key) {      return {       Id: modalId,       Title: options.title      }[key];     });     $('body').append(content);     var target = $('#' + modalId);     target.find('.modal-body').load(options.url);     if (options.onReady())      options.onReady.call(target);     target.modal();     target.on('shown.bs.modal', function (e) {      if (options.onReady(e))       options.onReady.call(target, e);     });     target.on('hide.bs.modal', function (e) {      $('body').find(target).remove();     });    }   }  }(); })(jQuery);

不瞭解組件封裝的朋友可以先看看相關文章。這裡我們的確認取消提示框主要用到了confirm這個屬性對應的方法。還是來看看如何調用吧:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一.開發工具規範: 1. 開發工具經項目負責人調試後統一確定。 2. 開發工具一經確定不允許集成任何非統一插件,若有需要,經項目負責人同意後統一為 項目組成員添加。 3. 開發工具的編碼格式不允許修改。 二.排版規範: 1. 關鍵詞(或變數)和操作符之間加一個空格。 例如:int iCont = 1 ...
  • 集合技術 作業 作業 猜數字游戲 猜數字游戲 /* * 猜數字游戲 */ public class HomeWork1 { public static void main(String[] args) { // 獲取被猜的那個數字,需要使用隨機數類產生 Random r = new Random() ...
  • Memory leak patterns in JavaScript Handling circular references in JavaScript applications Abhijeet Bhattacharya and Kiran Shivarama SundarPublished o ...
  • 以下分析均採取沙箱模式 js (function (window) { //為了提高性能把需要的變數統一提前聲明 var arr = [], push = arr.push; //為區別jQuery,此文章以iQuery來定義封裝的函數 function iQuery( selector ) { r ...
  • echarts是百度的一個圖表插件,確實好用美觀。 之前實習接觸到的頁面大多是下麵這種調用方式 這次有一個頁面需要計時器反覆載入新數據,然後重繪echarts圖表。一開始我還是使用了上面這種方式,沒有發現太大問題。第二天早上來調試的時候,網頁運行了一段時間,我的電腦記憶體達到了56%,我是8G的記憶體, ...
  • 今天博客園註冊成功了,很是欣喜,趕緊在博客園留下我的足記。 半年前,我還是剛走出校園的一枚IT小白,對於IT行業什麼都不懂,我大學的專業是採礦工程,接觸過的編程語言只有VB,但說實話,那時候啥也不懂,考試嗎,相信大家也都知道,在考試前老師一般會畫原題,你死記硬背就行了,根本不用懂那行代碼什麼意思,只 ...
  • 經測試,相容IE8 ...
  • 1.Html區塊元素 HTML可以通過<div>和<span>將元素組合起來 大多數HTML元素被定義為塊級元素或內聯元素, 而塊級元素在瀏覽器顯示時,通常會以新行來開始(或結束)。如:<h1>,<p>,<ul>,<table> 內聯元素在顯示時通常不會以新行開始。如:<b>,<td>,<a>,<i ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...