jQuery基于函數(shù)重載實(shí)現(xiàn)自定義Alert函數(shù)樣式的方法
本文實(shí)例講述了jQuery基于函數(shù)重載實(shí)現(xiàn)自定義Alert函數(shù)樣式的方法。分享給大家供大家參考,具體如下:
(function(){
window.alert = function(text) {
text=text.toString().replace(/\\/g,'\\').replace(/\n/g,'<br />').replace(/\r/g,'<br />'); //解析alert內(nèi)容中的換行符
var alertdiv='<div id="alertdiv" style="position:absolute; display:none ; overflow:hidden; padding:10px 10px 8px; top: 50%; left: 50%; text-align:center; line-height:22px; background-color:#DDE4EE; border:1px solid #ccc">'+text+'<br /><input type="submit" name="button" id="button" value="確定" style="margin-top:8px;" onclick="$(this).parent().remove(); "/></div>'; //自定義div彈窗
$(document.body).append(alertdiv); //動(dòng)態(tài)加載div
$("#alertdiv").css({"margin-left":$("#alertdiv").width()/2*(-1)-20,"margin-top":$("#alertdiv").height()/2*(-1)-20}); //設(shè)置偏移數(shù)值,實(shí)現(xiàn)div居中
$("#alertdiv").show(); //顯示
};
})();
注:以上代碼可自適應(yīng)alert內(nèi)容長(zhǎng)度,支持換行符,調(diào)用時(shí)與原始alert函數(shù)無(wú)異。
完整示例代碼如下:
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.10.2/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
(function(){
window.alert = function(text) {
text=text.toString().replace(/\\/g,'\\').replace(/\n/g,'<br />').replace(/\r/g,'<br />'); //解析alert內(nèi)容中的換行符
var alertdiv='<div id="alertdiv" style="position:absolute; display:none ; overflow:hidden; padding:10px 10px 8px; top: 50%; left: 50%; text-align:center; line-height:22px; background-color:#DDE4EE; border:1px solid #ccc">'+text+'<br /><input type="submit" name="button" id="button" value="確定" style="margin-top:8px;" onclick="$(this).parent().remove(); "/></div>'; //自定義div彈窗
$(document.body).append(alertdiv); //動(dòng)態(tài)加載div
$("#alertdiv").css({"margin-left":$("#alertdiv").width()/2*(-1)-20,"margin-top":$("#alertdiv").height()/2*(-1)-20}); //設(shè)置偏移數(shù)值,實(shí)現(xiàn)div居中
$("#alertdiv").show(); //顯示
};
})();
</script><input type="submit" name="button" id="button" value="點(diǎn)擊彈出alert自定義窗口" onclick='alert("這是alert彈窗\n支持\\n換行符")'/>
運(yùn)行效果截圖如下:

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jquery中Ajax用法總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- JQuery的Alert消息框插件使用介紹
- 自編jQuery插件實(shí)現(xiàn)模擬alert和confirm
- jquery.alert 彈出式復(fù)選框?qū)崿F(xiàn)代碼
- jQuery綁定事件不執(zhí)行但alert后可以正常執(zhí)行
- jQuery提示插件alertify使用指南
- jquery SweetAlert插件實(shí)現(xiàn)響應(yīng)式提示框
- jQuery彈出(alert)select選擇的值
- 使用jQuery實(shí)現(xiàn)更改默認(rèn)alert框體
- 基于jquery ui的alert,confirm方案(支持換膚)
- jquery模擬alert的彈窗插件
- jquery插件hiAlert實(shí)現(xiàn)網(wǎng)頁(yè)對(duì)話(huà)框美化
相關(guān)文章
easyui Droppable組件實(shí)現(xiàn)放置特效
droppable和draggable有類(lèi)似的地方,不過(guò)區(qū)別點(diǎn)在于前者著重于將元素放進(jìn)某個(gè)容器中,而后者則著重于可拖拽(雖然可能一些效果兩者都可以實(shí)現(xiàn))。而且通過(guò)查看easyloader源碼可知道,droppable并不依賴(lài)于draggable。2015-08-08
jQuery實(shí)現(xiàn)響應(yīng)鼠標(biāo)背景變化的動(dòng)態(tài)菜單效果代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)響應(yīng)鼠標(biāo)背景變化的動(dòng)態(tài)菜單效果代碼,涉及jquery鼠標(biāo)mouseover事件操作頁(yè)面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
jQuery頭像裁剪工具jcrop用法實(shí)例(附演示與demo源碼下載)
這篇文章主要介紹了jQuery頭像裁剪工具jcrop用法,結(jié)合實(shí)例形式分析了jQuery頭像裁剪工具jquery.jcrop.js具體使用技巧,并附帶了完整的demo源碼供讀者下載參考,需要的朋友可以參考下2016-01-01
jquery實(shí)現(xiàn)移動(dòng)端按鈕組左右滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)移動(dòng)端按鈕組左右滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
簡(jiǎn)單實(shí)現(xiàn)jQuery進(jìn)度條輪播實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)單實(shí)現(xiàn)jQuery進(jìn)度條輪播實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
artDialog 4.1.5 Dreamweaver代碼提示/補(bǔ)全插件 附下載
artDialog是一個(gè)輕巧且高度兼容的javascript對(duì)話(huà)框組件,可讓你的網(wǎng)頁(yè)交互擁有桌面軟件般的用戶(hù)體驗(yàn)2012-07-07
jQuery EasyUI 中文API Layout(Tabs)
發(fā)布jQuery EasyUI 中文API—Layout(Tabs)總結(jié),需要的朋友可以參考下。2010-04-04
jQuery實(shí)現(xiàn)帶進(jìn)度條的輪播圖
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)帶進(jìn)度條的輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09

