用jQuery模擬select下拉框的簡單示例代碼
很多時(shí)候,美工會(huì)覺得默認(rèn)的select下拉框很難看(特別是右側(cè)的下拉箭頭按鈕),他們通常喜歡用一個(gè)自定義的圖標(biāo)來代替這個(gè)按鈕。這樣就只能用 js + div 來模擬了,倒騰了一番,用jQuery模擬了下,當(dāng)然網(wǎng)上這種文章也不少,只是懶得去看找
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>自己實(shí)現(xiàn)的下拉框</title>
<style type="text/css" media="all">
*{font-size:12px;line-height:18px;list-style:none;padding:0;margin:0;text-decoration:none;color:#000;border:0}
.page{text-align:center;margin:50px;}
input{border-bottom:solid 1px #ccc;height:18px}
.expand{display:none;position:absolute;width:200px;height:100px;overflow-y:auto;border:solid 1px #ccc};
.expand li{margin:1px 0;background:#fff}
.expand a{text-decoration:none;display:block;padding:0 5px;background:#efefef;margin:1px 0}
.expand a:hover{background:#ff9}
</style>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.min.js"></script>
<script type="text/javascript">
function showExpand(targetId, expandId, expand_class) {
//先關(guān)掉其它彈出的層
if (expand_class != undefined) {
$("." + expand_class).hide();
}
//判斷是否為IE
var isIE = (! +[1, ]);
var expand = $("#" + expandId);
var target = $("#" + targetId);
var dx = 0;
if (isIE) {
dx = -2;
}
else {
dx = 0;
}
expand.get(0).style.left = target.get(0).getBoundingClientRect().left + dx + "px";
if (isIE) {
dx = 17;
}
else {
dx = 19;
}
expand.get(0).style.top = parseInt(target.get(0).getBoundingClientRect().top) + dx + "px";
expand.show();
//每個(gè)li點(diǎn)擊時(shí)賦值
$("#" + expandId).find("li").each(function (i) {
$(this).show().click(function () {
target.val($(this).text().split(' ')[1]);
expand.hide();
})
})
}
function search(srcId, expandId) {
var expand = $("#" + expandId);
var src = $("#" + srcId);
var A = expand.find("a");
var v = src.val().toUpperCase();
A.each(function (i) {
if (v.length >= 2) {
if ($(this).text().toUpperCase().indexOf(v) == -1) {
$(this).parent().hide();
}
else {
$(this).parent().show();
}
}
if (v.length <= 0) {
$(this).parent().show();
}
})
src.val(v);
}
$().ready(function(){
$("#txt_city").keyup(function(){
search('txt_city','city_select1');
}).focus(function(){
showExpand('txt_city','city_select1','expand')
})
$("#txt_city2").keyup(function(){
search('txt_city2','city_select2');
}).focus(function(){
showExpand('txt_city2','city_select2','expand')
})
})
function fnTest(){
document.getElementById("txtTarget").value = document.getElementById("txtSrc").value;
}
</script>
</head>
<body>
<div class="page" style="text-align: center">
<input type="text" value="" id="txt_city" class="input_expand" /><a
href="#" onclick="showExpand('txt_city','city_select1','expand')">▼</a>
<div class="expand" id="city_select1">
<ul>
<li><a href="javascript:void(0)">SH 上海</a></li>
<li><a href="javascript:void(0)">BJ 北京</a></li>
<li><a href="javascript:void(0)">HZ 杭州</a></li>
<li><a href="javascript:void(0)">WH 武漢</a></li>
<li><a href="javascript:void(0)">NJ 南京</a></li>
<li><a href="javascript:void(0)">WX 無錫</a></li>
</ul>
</div>
<input type="text" value="" id="txt_city2" class="input_expand" /><a
href="#" onclick="showExpand('txt_city2','city_select2','expand')">▼</a>
<div class="expand" id="city_select2">
<ul>
<li><a href="javascript:void(0)">CN 中文</a></li>
<li><a href="javascript:void(0)">EN 英語</a></li>
<li><a href="javascript:void(0)">JP 日本</a></li>
<li><a href="javascript:void(0)">RA 俄語</a></li>
<li><a href="javascript:void(0)">FA 法語</a></li>
<li><a href="javascript:void(0)">00 其它</a></li>
</ul>
</div>
<br/>
<br/>
<input type="text" id="txtSrc" onkeyup="fnTest()" />
<br/>
<input type="text" id="txtTarget" />
</div>
</body>
</html>
無圖無真相,真相在此:

不足之處:
1、按鍵盤上下鍵時(shí),沒有高亮的自動(dòng)移動(dòng)
2、鍵入文字自動(dòng)過濾結(jié)果時(shí),感覺相對(duì)原生的select有些不自然
- jquery模擬SELECT下拉框取值效果
- jQuery 文本框模擬下拉列表效果
- 用jQuery實(shí)現(xiàn)的模擬下拉框代碼
- jQuery模擬下拉框選擇對(duì)應(yīng)菜單的內(nèi)容
- jquery 模擬類搜索框自動(dòng)完成搜索提示功能(改進(jìn))
- 模擬多級(jí)復(fù)選框效果的jquery代碼
- jquery模擬多級(jí)復(fù)選框效果的簡單實(shí)例
- jquery 模擬雅虎首頁的點(diǎn)擊對(duì)話框效果
- jQuery模擬12306城市選擇框功能簡單實(shí)現(xiàn)方法示例
- jQuery模擬html下拉多選框的原生實(shí)現(xiàn)方法示例
相關(guān)文章
jQuery實(shí)現(xiàn)列表自動(dòng)循環(huán)滾動(dòng)鼠標(biāo)懸停時(shí)停止?jié)L動(dòng)
需要在頁面中一個(gè)小的區(qū)域循環(huán)滾動(dòng)展示新聞并且鼠標(biāo)懸停時(shí)停止?jié)L動(dòng)并提示,離開后,繼續(xù)滾動(dòng),具體實(shí)現(xiàn)如下,喜歡的朋友可以參考下2013-09-09
jquery 模擬類搜索框自動(dòng)完成搜索提示功能(改進(jìn))
改進(jìn)版,支持多個(gè)輸入框!前面因?yàn)槎鄠€(gè)輸入框信息不好保存所以只能支持一個(gè),現(xiàn)在用輸入框的alt屬性來保存修改前的內(nèi)容,所以能支持多個(gè)輸入框了.初步測試沒發(fā)現(xiàn)問題,歡迎大家一起測試改進(jìn)!!!2010-05-05
解決jquery1.9不支持browser對(duì)象的問題
項(xiàng)目里面一直用的都是jquery1.9,不支持browser對(duì)象,百度一下,找到了解決方案在此與大家分享下,有遇到此問題的朋友不要錯(cuò)過2013-11-11
iframe里面的元素觸發(fā)父窗口元素事件的jquery代碼
這篇文章主要介紹了jquery:iframe里面的元素怎樣觸發(fā)父窗口元素的事件,很簡單,但很實(shí)用,需要的朋友可以看看2014-10-10
LazyForm jQuery plugin 定制您的CheckBox Radio和Select
LazyForm jQuery plugin 定制您的CheckBox Radio和Select ,需要的朋友可以參考下。2009-10-10
jQuery Mobile的loading對(duì)話框顯示/隱藏方法分享
jQuery Mobile提供兩個(gè)方法,使得開發(fā)者在編寫JavaScript業(yè)務(wù)編輯時(shí),可以隨意的控制loading提示框2013-11-11

