jquery 彈出層實(shí)現(xiàn)代碼
更新時(shí)間:2009年10月30日 13:11:58 作者:
在項(xiàng)目中遇到一個(gè)問題,本來以為是使用dropdownlist的效果的,仔細(xì)一看,其實(shí)是在點(diǎn)擊文本框以后,在其正下方彈出一個(gè)層,里邊有多個(gè)選項(xiàng)進(jìn)行選擇。很像jQuery.datepicker 的效果。
經(jīng)過分析,網(wǎng)上的參考,終于搞定了~~
jQuery插件代碼:
jQuery.fn.selectItem = function(targetId) {
var _seft = this;
var targetId = $(targetId);
this.toggle(
function() {
var A_top = $(this).offset().top + $(this).outerHeight(true); // 1
var A_left = $(this).offset().left;
targetId.bgiframe();
targetId.show().css({ "position": "absolute", "top": A_top + "px", "left": A_left + "px" });
}
, function() {
targetId.hide();
}
);
targetId.find("#selectItemClose").click(function() {
targetId.hide();
});
targetId.find("#selectSub :checkbox").click(function() {
targetId.find(":checkbox").attr("checked", false);
$(this).attr("checked", true);
_seft.val($(this).val());
targetId.hide();
});
$(document).click(function(event) {
if (event.target.id != _seft.selector.substring(1)) {
targetId.hide();
}
});
targetId.click(function(e) {
e.stopPropagation();
});
return this;
}
HTML代碼:
<input type="text" id="txtCompetencies" value="All" readonly="readonly"/>
<div id="selectCompetencies">
<table>
<tbody>
<tr><td><input type="checkbox" />All</td></tr>
<tr><td><input type="checkbox" />Confidence</td></tr>
</tbody>
</table>
</div>
css樣式:
/* pop div begin */
.selectItemcont{padding:8px;}
.selectItemtit{line-height:20px;height:20px;margin:1px;padding-left:12px;}
.bgc_ccc{background:#E88E22;}
.selectItemleft{float:left;margin:0px;padding:0px;font-size:12px;font-weight:bold;color:#fff;}
.selectItemright{float:right;cursor:pointer;color:#fff;}
.selectItemcls{clear:both;font-size:0px;height:0px;overflow:hidden;}
.selectItemhidden{display:none;}
#selectCompetencies
{
display:none;
background:#FFF;
position:absolute;
top:0px;
left:center;
border:1px solid #000;
overflow-x:hidden;
overflow-y:auto;
width:250px;
z-index:1000;
max-height:200px;
}
/* pop div end */
JavaScript代碼:
$("#txtCompetencies").selectItem("#selectCompetencies");
實(shí)現(xiàn)點(diǎn)擊文本框,在其正下方彈出一個(gè)新的層,再次點(diǎn)擊的時(shí)候,隱藏層。
后面的話,我會(huì)把這個(gè)插件繼續(xù)完善的~~~ O(∩_∩)O...
jQuery插件代碼:
復(fù)制代碼 代碼如下:
jQuery.fn.selectItem = function(targetId) {
var _seft = this;
var targetId = $(targetId);
this.toggle(
function() {
var A_top = $(this).offset().top + $(this).outerHeight(true); // 1
var A_left = $(this).offset().left;
targetId.bgiframe();
targetId.show().css({ "position": "absolute", "top": A_top + "px", "left": A_left + "px" });
}
, function() {
targetId.hide();
}
);
targetId.find("#selectItemClose").click(function() {
targetId.hide();
});
targetId.find("#selectSub :checkbox").click(function() {
targetId.find(":checkbox").attr("checked", false);
$(this).attr("checked", true);
_seft.val($(this).val());
targetId.hide();
});
$(document).click(function(event) {
if (event.target.id != _seft.selector.substring(1)) {
targetId.hide();
}
});
targetId.click(function(e) {
e.stopPropagation();
});
return this;
}
HTML代碼:
復(fù)制代碼 代碼如下:
<input type="text" id="txtCompetencies" value="All" readonly="readonly"/>
<div id="selectCompetencies">
<table>
<tbody>
<tr><td><input type="checkbox" />All</td></tr>
<tr><td><input type="checkbox" />Confidence</td></tr>
</tbody>
</table>
</div>
css樣式:
復(fù)制代碼 代碼如下:
/* pop div begin */
.selectItemcont{padding:8px;}
.selectItemtit{line-height:20px;height:20px;margin:1px;padding-left:12px;}
.bgc_ccc{background:#E88E22;}
.selectItemleft{float:left;margin:0px;padding:0px;font-size:12px;font-weight:bold;color:#fff;}
.selectItemright{float:right;cursor:pointer;color:#fff;}
.selectItemcls{clear:both;font-size:0px;height:0px;overflow:hidden;}
.selectItemhidden{display:none;}
#selectCompetencies
{
display:none;
background:#FFF;
position:absolute;
top:0px;
left:center;
border:1px solid #000;
overflow-x:hidden;
overflow-y:auto;
width:250px;
z-index:1000;
max-height:200px;
}
/* pop div end */
JavaScript代碼:
復(fù)制代碼 代碼如下:
$("#txtCompetencies").selectItem("#selectCompetencies");
實(shí)現(xiàn)點(diǎn)擊文本框,在其正下方彈出一個(gè)新的層,再次點(diǎn)擊的時(shí)候,隱藏層。
后面的話,我會(huì)把這個(gè)插件繼續(xù)完善的~~~ O(∩_∩)O...
相關(guān)文章
jQuery實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁(yè)換膚效果示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁(yè)換膚效果,涉及jQuery事件響應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)變換操作技巧,需要的朋友可以參考下2016-09-09jQuery validate(submitHandler函數(shù))驗(yàn)證通過發(fā)送Ajax(實(shí)例詳解)
這篇文章主要介紹了jQuery validate(submitHandler函數(shù))驗(yàn)證通過發(fā)送Ajax,可以用validate框架驗(yàn)證元素,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-10-10jquery.validate自定義驗(yàn)證用法實(shí)例分析【成功提示與擇要提示】
這篇文章主要介紹了jquery.validate自定義驗(yàn)證用法,結(jié)合實(shí)例形式分析了jQuery成功提示與擇要提示驗(yàn)證操作相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2020-06-06Jquery ajax不能解析json對(duì)象,報(bào)Invalid JSON錯(cuò)誤的原因和解決方法
我們知道Invalid JSON錯(cuò)誤導(dǎo)致的json對(duì)象不能解析,一般都是服務(wù)器返回的json字符串的語(yǔ)法有錯(cuò)誤。這種情況下,我們只需要仔細(xì)的檢查一下json就可以解決問題。2010-03-03JQuery調(diào)用WebServices的方法和4個(gè)實(shí)例
你是不是經(jīng)常作這種開發(fā),前端用JS寫邏輯,后端用aspx或者ashx作服務(wù)?你是不是經(jīng)常在請(qǐng)求aspx的時(shí)候在查詢字符串中拼接諸如a.aspx?method=getDepartmetn¶m1=1¶m2=2的字符串?2014-05-05jQuery中hover與mouseover和mouseout的區(qū)別分析
這篇文章主要介紹了jQuery中hover與mouseover和mouseout的區(qū)別,結(jié)合實(shí)例分析了jQuery中hover與mouseover和mouseout的區(qū)別與使用技巧,需要的朋友可以參考下2015-12-12