Use jQuery to change the default alert box _ jquery-js tutorial

Source: Internet
Author: User
This article describes how to use jquery to rewrite the style of the Alert pop-up box. It is very practical. We recommend it to you here. If you need it, you can refer to it. To change the frame, you must modify the built-in control winpop. The following is the specific code of winpop.

(Function (window, jQuery, undefined) {var HTMLS = {ovl :'

'+'

'+'

'+'

'+'

', Alert :'', Confirm :''+''} Function Winpop () {var config = {}; this. get = function (n) {return config [n];} this. set = function (n, v) {config [n] = v;} this. init ();} Winpop. prototype = {init: function () {this. createDom (); this. bindEvent () ;}, createDom: function () {var body = jQuery ("body"), ovl = jQuery ("# J_WinpopBox"); if (ovl. length = 0) {body. append (HTMLS. ovl);} this. set ("ovl", jQuery ("# J_WinpopBox"); this. Set ("mask", jQuery ("# J_WinpopMask") ;}, bindEvent: function () {var _ this = this, ovl = _ this. get ("ovl"), mask = _ this. get ("mask"); ovl. on ("click ",". j_AltBtn ", function (e) {_ this. hide () ;}); ovl. on ("click ",". j_CfmTrue ", function (e) {var cb = _ this. get ("confirmBack"); _ this. hide (); cb & cb (true) ;}); ovl. on ("click ",". j_CfmFalse ", function (e) {var cb = _ this. get ("confirmBack"); _ this. hide (); Cb & cb (false) ;}); mask. on ("click", function (e) {_ this. hide () ;}); jQuery (document ). on ("keyup", function (e) {var kc = e. keyCode, cb = _ this. get ("confirmBack"); if (kc = 27) {_ this. hide ();} else if (kc = 13) {_ this. hide (); if (_ this. get ("type") = "confirm") {cb & cb (true) ;}}) ;}, alert: function (str, btnstr) {var str = typeof str = 'string '? Str: str. toString (), ovl = this. get ("ovl"); this. set ("type", "alert"); ovl. find (". j_WinpopMain "cmd.html (str); if (typeof btnstr =" undefined ") {ovl. find (". j_WinpopBtns "cmd.html (HTMLS. alert);} else {ovl. find (". j_WinpopBtns "cmd.html (btnstr);} this. show () ;}, confirm: function (str, callback) {var str = typeof str === 'string '? Str: str. toString (), ovl = this. get ("ovl"); this. set ("type", "confirm"); ovl. find (". j_WinpopMain "cmd.html (str); ovl. find (". j_WinpopBtns "cmd.html (HTMLS. confirm); this. set ("confirmBack", (callback | function () {})); this. show () ;}, show: function () {this. get ("ovl "). show (); this. get ("mask "). show () ;}, hide: function () {var ovl = this. get ("ovl"); ovl. find (". j_WinpopMain "cmd.html (" "); ovl. find (". j_WinpopBtns "pai.html (" "); ovl. hide (); this. get ("mask "). hide () ;}, destory: function () {this. get ("ovl "). remove (); this. get ("mask "). remove (); delete window. alert; delete window. confirm ;}}; var obj = new Winpop (); window. alert = function (str) {obj. alert. call (obj, str) ;}; window. confirm = function (str, cb) {obj. confirm. call (obj, str, cb) ;};} (window, jQuery );

Then instantiate the object

Var obj = new Winpop (); // create a Winpop Instance Object // overwrite the alert control window. alert = function (str) {obj. alert. call (obj, str) ;}; // overwrite the confirm control window. confirm = function (str, cb) {obj. confirm. call (obj, str, cb );};

The following JS is indispensable

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.