基于jquery的網(wǎng)頁(yè)SELECT下拉框美化代碼
2.允許多次調(diào)用 $("...").selectCss(),以解決Select的options更新后無(wú)法同步的問(wèn)題。
使用方法如下:
<!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=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<link href="selectCss.css" rel="stylesheet" type="text/css" />
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="selectCss.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select").selectCss();
});
</script>
</head>
<body>
<select name="" onchange="alert(this.value)" id="select1">
<option value="1" title="選項(xiàng)選項(xiàng)選項(xiàng)選項(xiàng)" >選項(xiàng)1</option>
<option value="2">選項(xiàng)2</option>
<option value="3">選項(xiàng)3</option>
</select>
<select name="" id="select2">
<option value="1">選項(xiàng)31</option>
<option value="2">選項(xiàng)32</option>
<option value="3">選33</option>
</select>
</body>
</html>
主要文件包括 selectCss.css 和 selectCss.js
selectCss.js 文件代碼:
(function($){
function hideOptions(speed){
if(speed.data){speed=speed.data}
if($(document).data("nowselectoptions"))
{
$($(document).data("nowselectoptions")).slideUp(speed);
$($(document).data("nowselectoptions")).prev("div").removeClass("tag_select_open");
$(document).data("nowselectoptions",null);
$(document).unbind("click",hideOptions);
$(document).unbind("keyup",hideOptionsOnEscKey);
}
}
function hideOptionsOnEscKey(e){
var myEvent = e || window.event;
var keyCode = myEvent.keyCode;
if(keyCode==27)hideOptions(e.data);
}
function showOptions(speed){
$(document).bind("click",speed,hideOptions);
$(document).bind("keyup",speed,hideOptionsOnEscKey);
$($(document).data("nowselectoptions")).slideDown(speed);
$($(document).data("nowselectoptions")).prev("div").addClass("tag_select_open");
}
$.fn.selectCss=function(_speed){
$(this).each(function(){
var speed=_speed||"fast";
if($(this).data("cssobj")){
$($(this).data("cssobj")).remove();
}
$(this).hide();
var divselect = $("<div></div>").insertAfter(this).addClass("tag_select");
$(this).data("cssobj",divselect);
var divoptions = $("<ul></ul>").insertAfter(divselect).addClass("tag_options").hide();
divselect.click(function(e){
if($($(document).data("nowselectoptions")).get(0) != $(this).next("ul").get(0)){
hideOptions(speed);
}
if(!$(this).next("ul").is(":visible"))
{
e.stopPropagation();
$(document).data("nowselectoptions",$(this).next("ul"));
showOptions(speed);
}
});
divselect.hover(function(){
$(this).addClass("tag_select_hover");
}
,
function(){
$(this).removeClass("tag_select_hover");
});
$(this).change(function(){
$(this).nextAll("ul").children("li:eq("+ $(this)[0].selectedIndex +")").addClass("open_selected").siblings().removeClass("open_selected");
$(this).next("div").html($(this).children("option:eq("+ $(this)[0].selectedIndex +")").text());
});
$(this).children("option").each(function(i){
var lioption= $("<li></li>").html($(this).text()).attr("title",$(this).attr("title")).appendTo(divoptions);
if($(this).attr("selected")){
lioption.addClass("open_selected");
divselect.html($(this).text());
}
lioption.data("option",this);
lioption.click(function(){
lioption.data("option").selected=true;
$(lioption.data("option")).trigger("change",true)
});
lioption.hover(
function(){$(this).addClass("open_hover");},
function(){ $(this).removeClass("open_hover"); }
);
});
});
}
})(jQuery);
selectCss.Css 文件代碼:
.tag_select{display:block;color:#000;width:179px;height:23px;background:transparent url("images/index_22.jpg") no-repeat 0 0;padding:0 10px;line-height:23px; color:#7D7D7D; font-size:12px; cursor:pointer}
.tag_select_hover{ color:#ff0000; background:transparent url("selectbg.jpg") no-repeat 0 0; }
.tag_select_open{ color:#0000ff; background:transparent url("selectbg.jpg") no-repeat 0 0;}
ul.tag_options{position:absolute;margin:0;list-style:none;background:#fff;padding:0 0 1px;margin:0;width:162px ; height:100px; overflow:hidden; overflow-y:auto; font-size:12px; margin-left:10px; cursor:pointer; z-index:1000 }
ul.tag_options li{background:#fff; display:block;width:140px;padding:0 10px;height:20px;text-decoration:none;line-height:20px;color:#000; font-weight:normal; font-size:12px}
ul.tag_options li.open_hover{background:#0000ff;color:#000; font-weight:normal; font-size:12px}
ul.tag_options li.open_selected{background:#ccc; font-size:12px;font-weight:bold; }
selectbg.jpg 圖片:

- 利用jQuery實(shí)現(xiàn)可輸入搜索文字的下拉框
- jquery獲得下拉框值的代碼
- jquery動(dòng)態(tài)加載select下拉框示例代碼
- JQuery實(shí)現(xiàn)級(jí)聯(lián)下拉框效果實(shí)例講解
- 基于jquery的無(wú)限級(jí)聯(lián)下拉框js插件
- JQuery打造省市下拉框聯(lián)動(dòng)效果
- Jquery實(shí)現(xiàn)顯示和隱藏的4種簡(jiǎn)單方式
- JQuery顯示、隱藏div的幾種方法簡(jiǎn)明總結(jié)
- jQuery控制TR顯示隱藏的幾種方法
- JQuery顯示隱藏頁(yè)面元素的方法總結(jié)
- jQuery設(shè)置下拉框顯示與隱藏效果的方法分析
相關(guān)文章
jQuery ajax BUG:object doesn''t support this property or met
使用$.ajax時(shí)出現(xiàn)的錯(cuò)誤,IE7下才會(huì)出錯(cuò),IE6,IE8都正常。2010-07-07jQuery+CSS3折疊卡片式下拉列表框?qū)崿F(xiàn)效果
這是一款使用jQuery和CSS3制作的效果非常炫酷的折疊卡片式下拉列表框特效,感興趣的小伙伴們可以參考一下2015-11-11基于jQuery的輸入框在光標(biāo)位置插入內(nèi)容, 并選中
基于jQuery的輸入框在光標(biāo)位置插入內(nèi)容, 并選中功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-10-10Domino中運(yùn)用jQuery讀取視圖內(nèi)容的方法
jQuery是繼prototype之后又一個(gè)優(yōu)秀的Javascrīpt框架。其宗旨是——WRITE LESS,DO MORE,寫(xiě)更少的代碼,做更多的事情。2009-10-10詳解jQuery中的getAll()和cleanData()
這篇文章主要介紹了jQuery之getAll()和cleanData()函數(shù)的實(shí)例代碼解析,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-04-04jQuery學(xué)習(xí)筆記 更改jQuery對(duì)象
jQuery提供了一些方法,使流程變?yōu)樯蒵Query對(duì)象A,操作jQuery對(duì)象A;更改為jQuery對(duì)象B,操作jQuery對(duì)象B2012-09-09jquery輪播的實(shí)現(xiàn)方式 附完整實(shí)例
這篇文章主要介紹了jquery輪播的實(shí)現(xiàn)方式,文中分享了jQuery輪播完整實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Jquery圖形報(bào)表插件 jqplot簡(jiǎn)介及參數(shù)詳解
jqPlot是 一款基于jquery類(lèi)庫(kù)的圖標(biāo)繪制插件。通過(guò)jqPlot可以再網(wǎng)頁(yè)中繪制線狀、柱狀、餅狀等多種樣式圖表。而且,jqPlot具有插件可擴(kuò)展性 (Pluggability),你可以編寫(xiě)自己的圖表樣式2012-10-10jQuery 如何實(shí)現(xiàn)一個(gè)滑動(dòng)按鈕開(kāi)關(guān)
本文給大家分享一段jquery代碼實(shí)現(xiàn)滑動(dòng)按鈕開(kāi)關(guān)的效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的的朋友參考下2016-12-12