jQuery彈出層插件Thickbox使用心得
Thickbox官方網(wǎng)站(上面有例子和基本的使用方法):http://jquery.com/demo/thickbox/
就我使用過(guò)程中,Thickbox常見(jiàn)問(wèn)題:
1。跨iframe的彈出層。
癥狀:每次thickbox都只在frame中彈出,而不會(huì)整個(gè)屏幕覆蓋
原因和解決方法:
thickbox使用tb_show()函數(shù)在body后面加入彈出層??梢允褂脀indow.top.tb_show()把彈出層加到頁(yè)面上。我的tihickbox插件中修改如下:在tb_init()中把tb_show(t,a,g)替換如下
- if(a.indexOf('TB_iniframe')!=-1)
- {
- window.top.tb_show(t,a,g);
- }
- else
- {
- tb_show(t,a,g);
- }
這樣只只要在原來(lái)的鏈接上加入TB_iniframe=true即可,如div.aspx?height=180&width=400&TB_iframe=true&TB_iniframe=true&modal=true
2.thickbox只支持一層彈出,不可支持多層彈出。
修改過(guò)的控件已經(jīng)支持(不足:ie6下失效彈出層失效了,占時(shí)沒(méi)解決,哈哈)
3.彈出層關(guān)閉后,文本框無(wú)法聚焦。
癥狀:關(guān)閉彈出層后,原來(lái)頁(yè)面上的文本框無(wú)法聚焦
原因和解決方法:這個(gè)的原因不好說(shuō),很多人都認(rèn)為是ie本身的bug。是由于iframe沒(méi)有移除,即使移除了。內(nèi)存上也么有清除造成的。這也是我猜的。哈哈。解決方法是在tb_remove()中先手動(dòng)移除iframe然后,在強(qiáng)制做垃圾回收,至少我是可以啦。哈哈。代碼如下:
1functiontb_remove(){
2varseq=PopSeq();
3$("#TB_imageOff"+seq).unbind("click");
4$("#TB_closeWindowButton"+seq).unbind("click");
5
6$("#TB_window"+seq).fadeOut("fast",function(){
7/**////手動(dòng)移除ifrmae,IE的一個(gè)bug
8$('#TB_iframeContent'+seq).remove();
9$('#TB_window'+seq+',#TB_overlay'+seq+',#TB_HideSelect'+seq).trigger("unload").unbind().remove();
10/**////自己調(diào)用垃圾回收,強(qiáng)制清楚iframe內(nèi)存,解決文本框無(wú)法輸入問(wèn)題。
11CollectGarbage();
12});
13if(typeofdocument.body.style.maxHeight=="undefined"){//ifIE6
14$("body","html").css({height:"auto",width:"auto"});
15$("html").css("overflow","");
16}
17document.onkeydown="";
18document.onkeyup="";
19returnfalse;
20}
4.在asp.net中如何動(dòng)態(tài)設(shè)置需要的參數(shù)和關(guān)閉彈出層。
癥狀:thickbox提供的例子都是需要在input后a的class加thickbox,而且參數(shù)什么都是固定的。而我們傳遞的參數(shù)一般需要?jiǎng)討B(tài)。
解決方法,使用asp.netajax,不多說(shuō)了。直接看代碼吧。
封裝一個(gè)popup類(lèi),
1publicclassPopup
2{
3/**////<summary>
4///showthepopupdiv
5///</summary>
6///<paramname="panel">containerthebutton</param>
7///<paramname="url"></param>
8publicstaticvoidShowPopup(UpdatePanelpanel,stringurl)
9{
10ScriptManager.RegisterClientScriptBlock(panel,panel.GetType(),"ShowPopup","ShowPopup('"+url+"')",true);
11}
12
13/**////<summary>
14///
15///</summary>
16///<paramname="panel"></param>
17///<paramname="page">requestpage</param>
18publicstaticvoidClosePopup(UpdatePanelpanel)
19{
20
21stringjs="self.parent.tb_remove();";
22
23ScriptManager.RegisterClientScriptBlock(panel,panel.GetType(),"closepopup",js,true);
24}
25}
需要的js
- functionShowPopup(url){
- window.top.tb_show(null,url,false);
- }
頁(yè)面上例子
1/**////add按鈕需要放在updatepanel里面
2protectedvoidbtnAdd_Click(objectsender,EventArgse)
3{
4/**////自己組參數(shù)
5stringurl="aa.aspx?height=180&width=400&Type="+ddlType.SelectedItem.Value;
6url+="&TB_iframe=true&TB_iniframe=true&modal=true";
7Popup.ShowPopup(this.upButtons,url);
8}
不足:由于現(xiàn)在我的不需要支持ie6。所以我也一直沒(méi)把我的插件改到支持ie6.如果有那個(gè)朋友修改好了麻煩共享一下。
【編輯推薦】