欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery)擴(kuò)展jQuery系列之一 模擬alert,confirm(一)

 更新時(shí)間:2010年12月04日 22:12:09   作者:  
很多人都為了使alert系統(tǒng)的調(diào)用函數(shù)在自己的控制范圍之內(nèi),都選擇了去封裝一個(gè)屬于自己的alert組件,現(xiàn)在我們就動(dòng)手實(shí)現(xiàn)一個(gè)這樣的小部件。
效果圖

全部代碼
復(fù)制代碼 代碼如下:

/**
* @author xing
*/
(function($){
$.extend({
alert:function(html,callback){
var dialog=new Dialog();
dialog.build('alert',callback,html);
},
confirm:function(html,callback){
var dialog=new Dialog();
dialog.build('confirm',callback,html);
}
});
var Dialog=function(){
var render={
template:' <div class="alertParent"><div class="alertContent"><h2 class="title">系統(tǒng)提示</h2><div class="alertHtml">你的操作出現(xiàn)錯(cuò)誤!</div><div class="btnBar"><input type="button" value="確定" id="sure"/></div></div></div>',
templateConfirm:' <div class="alertParent" id="confirmPanel"><div class="alertContent"><h2 class="title">系統(tǒng)提示</h2><div class="alertHtml">你的操作出現(xiàn)錯(cuò)誤!</div><div class="btnBar"><input type="button" value="取消" id="cancel"/><input type="button" value="確定" id="sure"/></div></div></div>',
/**
* 根據(jù)需要生成對話框
* @param {Object} type
* @param {Object} callback
* @param {Object} html
*/
renderDialog:function(type,callback,html){
if(type=='alert'){
this.renderAlert(callback,html);
}else{
this.renderConfirm(callback,html);
}
},
/**
* 生成alert
* @param {Object} callback
* @param {Object} html
*/
renderAlert:function(callback,html){
var temp=$(this.template).clone().hide();
temp.find('div.alertHtml').html(html);
$(document.body).append(temp);
this.setPosition(temp);
temp.fadeIn();
this.bindEvents('alert',temp,callback);
},
/**
* 生成confirm
* @param {Object} callback
* @param {Object} html
*/
renderConfirm:function(callback,html){
var temp=$(this.templateConfirm).clone().hide();
temp.find('div.alertHtml').html(html);
$(document.body).append(temp);
this.setPosition(temp);
temp.fadeIn();
this.bindEvents('confirm',temp,callback);
},
/**
* 設(shè)定對話框的位置
* @param {Object} el
*/
setPosition:function(el){
var width=el.width(),
height=el.height(),
pageSize=this.getPageSize();
el.css({
top:(pageSize.h-height)/2,
left:(pageSize.w-width)/2
});
},
/**
* 綁定事件
* @param {Object} type
* @param {Object} el
* @param {Object} callback
*/
bindEvents:function(type,el,callback){
if(type=="alert"){
if($.isFunction(callback)){
$('#sure').click(function(){
callback();
$('div.alertParent').remove();
});
}else{
$('#sure').click(function(){
$('div.alertParent').remove();
});
}
}else{
if($.isFunction(callback)){
$('#sure').click(function(){
callback();
$('div.alertParent').remove();
});
}else{
$('#sure').click(function(){
$('div.alertParent').remove();
});
}
$('#cancel').click(function(){
$('div.alertParent').remove();
});
}
},
/**
* 獲取頁面尺寸
*/
getPageSize:function(){
return {
w:document.documentElement.clientWidth,
h:document.documentElement.clientHeight
}
}
}
return {
build:function(type,callback,html){
render.renderDialog(type,callback,html);
}
}
}
})(jQuery);

因?yàn)槲覀兊腶lert,并不需要選擇器的支持,所以我們采用$.extend這樣聲明
復(fù)制代碼 代碼如下:

$.extend({
alert:function(html,callback){
},
confirm:function(html,callback){
}
});

其次我們聲明一個(gè)單體來生成這個(gè)組件到頁面,這個(gè)單體返回一個(gè)公共的方法build來創(chuàng)建這個(gè)組件
復(fù)制代碼 代碼如下:

var Dialog=function(){
return {
build:function(type,callback,html){
render.renderDialog(type,callback,html);
}
}
}

接下來我們分別聲明組件的HTML字符串
復(fù)制代碼 代碼如下:

var render={<BR> template:' <div class="alertParent"><div class="alertContent"><h2 class="title">系統(tǒng)提示</h2><div class="alertHtml">你的操作出現(xiàn)錯(cuò)誤!
</div><div class="btnBar"><input type="button" value="確定" id="sure"/></div></div></div>',<BR> templateConfirm:' <div class="alertParent"
id="confirmPanel"><div class="alertContent"><h2 class="title">系統(tǒng)提示</h2><div class="alertHtml">你的操作出現(xiàn)錯(cuò)誤!</div><div class="btnBar"><input type="button" value="取消"
id="cancel"/><input type="button" value="確定" id="sure"/></div></div></div>'}<BR>

向里面填充方法
復(fù)制代碼 代碼如下:

/**
* 根據(jù)需要生成對話框
* @param {Object} type
* @param {Object} callback
* @param {Object} html
*/
renderDialog:function(type,callback,html){
if(type=='alert'){
this.renderAlert(callback,html);
}else{
this.renderConfirm(callback,html);
}
},
/**
* 生成alert
* @param {Object} callback
* @param {Object} html
*/
renderAlert:function(callback,html){
var temp=$(this.template).clone().hide();
temp.find('div.alertHtml').html(html);
$(document.body).append(temp);
this.setPosition(temp);
temp.fadeIn();
this.bindEvents('alert',temp,callback);
},
/**
* 生成confirm
* @param {Object} callback
* @param {Object} html
*/
renderConfirm:function(callback,html){
var temp=$(this.templateConfirm).clone().hide();
temp.find('div.alertHtml').html(html);
$(document.body).append(temp);
this.setPosition(temp);
temp.fadeIn();
this.bindEvents('confirm',temp,callback);
},
/**
* 設(shè)定對話框的位置
* @param {Object} el
*/
setPosition:function(el){
var width=el.width(),
height=el.height(),
pageSize=this.getPageSize();
el.css({
top:(pageSize.h-height)/2,
left:(pageSize.w-width)/2
});
},
/**
* 綁定事件
* @param {Object} type
* @param {Object} el
* @param {Object} callback
*/
bindEvents:function(type,el,callback){
if(type=="alert"){
if($.isFunction(callback)){
$('#sure').click(function(){
callback();
$('div.alertParent').remove();
});
}else{
$('#sure').click(function(){
$('div.alertParent').remove();
});
}
}else{
if($.isFunction(callback)){
$('#sure').click(function(){
callback();
$('div.alertParent').remove();
});
}else{
$('#sure').click(function(){
$('div.alertParent').remove();
});
}
$('#cancel').click(function(){
$('div.alertParent').remove();
});
}
},
/**
* 獲取頁面尺寸
*/
getPageSize:function(){
return {
w:document.documentElement.clientWidth,
h:document.documentElement.clientHeight
}
}

接下來就是對話框的實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

$.extend({
alert:function(html,callback){
var dialog=new Dialog();
dialog.build('alert',callback,html);
},
confirm:function(html,callback){
var dialog=new Dialog();
dialog.build('confirm',callback,html);
}
});

調(diào)用方法:
復(fù)制代碼 代碼如下:

$.confirm('確定刪除?',function(){
alert('cccc');
});

$.alert('我的電腦');

最后就是CSS與HTML 了
復(fù)制代碼 代碼如下:

div.alertParent{
padding:6px;
background:#ccc;
background:rgba(201,201,201,0.8);
width:auto;
position:absolute;
-moz-border-radius:3px;
font-size:12px;
top:50px;
left:50px;
}
div.alertContent{
background:#fff;
width:350px;
height:auto;
border:1px solid #999;
}
h2.title{
width:100%;
height:28px;
background:#0698E9;
text-indent:10px;
font-size:12px;
color:#fff;
line-height:28px;
margin:0;
}
div.alertHtml{
background:url(dtips.gif) 0 0 no-repeat;
height:50px;
margin:10px;
margin-left:30px;
text-indent:50px;
line-height:50px;
font-size:14px;
}
div.btnBar{
border-top:1px solid #c6c6c6;
background:#f8f8f8;
height:30px;
}
div.btnBar input{
background:url(sure.png) no-repeat;
border:0;
width:63px;
height:28px;
float:right;
margin-right:5px;
}


html
復(fù)制代碼 代碼如下:

<div class="alertParent"><BR><div class="alertContent"><BR><h2 class="title">系統(tǒng)提示</h2><BR><div class="alertHtml"><BR>你的操作出現(xiàn)錯(cuò)誤!<BR></div><BR> <div class="btnBar"><BR> <input
type="button" value="確定"/><BR></div><BR></div><BR> </div><BR>

高手勿笑,為了說明實(shí)現(xiàn)的方式,我并沒有仔細(xì)的去完善這段代碼,僅僅是在寫作的半小時(shí)內(nèi)寫出的,所以有很多地方?jīng)]有去思考,有很多的紕漏,并且以一個(gè)比較笨的方式實(shí)現(xiàn)了這個(gè)模擬的alert,不過下次我們將通過構(gòu)建Button的方式實(shí)現(xiàn)一個(gè)組件,會(huì)加入遮罩,ajax調(diào)用,iframe寬度高度自適應(yīng)等更強(qiáng)大的功能。

相關(guān)文章

最新評論