JavaScript SweetAlert插件實現(xiàn)超酷消息警告框
今天給大家推薦一款不錯的超酷消息警告框–SweetAlert;SweetAlert是一款不需要jQuery支持的原生js提示框,風(fēng)格類似bootstrap。它的提示框不僅美麗動人,并且允許自定義,支持設(shè)置提示框標(biāo)題、提示類型、內(nèi)容展示圖片、確認(rèn)取消按鈕文本、點擊后回調(diào)函數(shù)等。和傳統(tǒng)的alert相比:
SweetAlert 能在頁面自動居中,支持桌面環(huán)境,移動端和平板,并且高度自定義。接下來看看它的具體使用!
HTML
首先引入相應(yīng)的js和css,該插件不需要jQuery插件的支持:
<link rel="stylesheet" type="text/css" /> <script src="http://sandbox.runjs.cn/uploads/rs/499/ixc5vbvz/sweetalert.min.js"></script>
然后放置6個不同的按鈕:
<div class="demo_1"> 基本示例:<button>點擊這里</button> </div> <div class="demo_2"> 提示成功:<button>點擊這里</button> </div> <div class="demo_3"> 提示失敗:<button>點擊這里</button> </div> <div class="demo_4"> 提示確認(rèn):<button>點擊這里</button> </div> <div class="demo_5"> 定義內(nèi)容:<button>點擊這里</button> </div> <div class="demo_6"> 確認(rèn)輸入:<button>點擊這里</button> </div>
JavaScript
定義js事件:
$(function() { $(".demo_1 button").click(function() { swal("這是一個信息提示框!"); }); $(".demo_2 button").click(function() { swal("Good!", "彈出了一個操作成功的提示框", "success"); }); $(".demo_3 button").click(function() { swal("OMG!", "彈出了一個錯誤提示框", "error"); }); $(".demo_4 button").click(function() { swal({ title: "您確定要刪除嗎?", text: "您確定要刪除這條數(shù)據(jù)?", type: "warning", showCancelButton: true, closeOnConfirm: false, confirmButtonText: "是的,我要刪除", confirmButtonColor: "#ec6c62" }, function() { $.ajax({ url: "do.php", type: "DELETE" }).done(function(data) { swal("操作成功!", "已成功刪除數(shù)據(jù)!", "success"); }).error(function(data) { swal("OMG", "刪除操作失敗了!", "error"); }); }); }); $(".demo_5 button").click(function() { swal({ title: "Good!", text: '自定義<span style="color:red">圖片</span>、<a href="#">HTML內(nèi)容</a>。<br/>5秒后自動關(guān)閉。', imageUrl: "images/thumbs-up.jpg", html: true, timer: 5000, showConfirmButton: false }); }); $(".demo_6 button").click(function() { swal({ title: "輸入框來了", text: "這里可以輸入并確認(rèn):", type: "input", showCancelButton: true, closeOnConfirm: false, animation: "slide-from-top", inputPlaceholder: "填點東西到這里面吧" }, function(inputValue) { if (inputValue === false) return false; if (inputValue === "") { swal.showInputError("請輸入!"); return false } swal("棒極了!", "您填寫的是: " + inputValue, "success"); }); }); });
相關(guān)文章
JS動態(tài)增加刪除UL節(jié)點LI及相關(guān)內(nèi)容示例
這篇文章主要介紹了JS如何動態(tài)增加刪除UL節(jié)點LI及相關(guān)內(nèi)容,需要的朋友可以參考下2014-05-05Bootstrap禁用響應(yīng)式布局的實現(xiàn)方法
這篇文章主要介紹了Bootstrap禁用響應(yīng)式布局的實現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03javascript用函數(shù)實現(xiàn)對象的方法
這篇文章主要介紹了javascript用函數(shù)實現(xiàn)對象的方法,涉及javascript函數(shù)使用技巧,需要的朋友可以參考下2015-05-05基于JavaScript實現(xiàn)猜數(shù)字游戲代碼實例
這篇文章主要介紹了基于JavaScript實現(xiàn)猜數(shù)字游戲代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07