用javascript來實(shí)現(xiàn)select標(biāo)簽的美化的代碼
更新時(shí)間:2007年10月25日 11:16:54 作者:
論壇經(jīng)常有人會(huì)問到用CSS如何美化Select標(biāo)簽,其實(shí)但凡你看到很酷的都是用javascript來實(shí)現(xiàn)的。昨天試著做了一下,基本實(shí)現(xiàn)的初級(jí)功能。拿出來和大家一起分享一下。先可以看一下預(yù)覽效果:http://www.iwcn.net/demo/select。
【功能需求】
1、調(diào)用要方便,做好之后應(yīng)該像這樣:
程序代碼
function loadSelect(selectobj){
//傳入一個(gè)select對(duì)象就能將他的樣式美化
}
2、不改變?cè)斜韱雾?xiàng),表單的頁面代碼不去破壞:
程序代碼
<form name="f" onsubmit="getResult();">
<fieldset>
<legend>用戶注冊(cè)</legend>
<div>
<label for="username">帳號(hào)</label>
<input type="text" id="username" name="username" />
</div>
<div>
<label for="pwd">密碼</label>
<input type="password" name="pwd" id="pwd" />
</div>
<div>
<label for="province">省份</label>
<select id="province" name="province">
<option value="10">江西</option>
<option value="11">福建</option>
<option value="12">廣東</option>
<option value="13">浙江</option>
</select>
</div>
</fieldset>
<input type="submit" value="提交" name="btnSub" />
</form>
【實(shí)現(xiàn)思路】
第一步:將表單中的select隱藏起來。
為什么?很簡(jiǎn)單,因?yàn)檫@家伙太頑固了,用css根本搞不出來你想要的。所以把它殺掉。
第二步:用腳本找到select標(biāo)簽在網(wǎng)頁上的絕對(duì)位置。
我們?cè)谀莻€(gè)位置上用DIV標(biāo)簽做個(gè)假的、好看點(diǎn)的來當(dāng)他的替身。
第三步:用腳本把select標(biāo)簽中的值讀出來。
雖然藏起來了,但它里邊的options我們還有用呢,統(tǒng)統(tǒng)取過來。
第四步:當(dāng)用戶點(diǎn)擊select標(biāo)簽的替身,也就是div的時(shí)候。我們?cè)儆靡粋€(gè)div浮在上一個(gè)div的下邊,這個(gè)就是options的替身了。
大致上就是這樣了,接下來我們一步一步去實(shí)現(xiàn)它!
【準(zhǔn)備工作】
1、想好你要把select美化成什么樣子,并準(zhǔn)備好相應(yīng)的圖片。我準(zhǔn)備了兩張小圖,就是下拉箭頭1和下拉箭頭2,1是默認(rèn)樣式,2是鼠標(biāo)移過來的樣式。
2、寫好一個(gè)普通的表單遞交頁面,比如下邊這個(gè)。注意我給select定義了基本的CSS樣式、在頭部添加了調(diào)用js文件的代碼、在body中添加了調(diào)用函數(shù)的腳本。
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
【編寫javascript】
程序代碼
<script type="text/javascript" src="select.js"></script>
新建一個(gè)js文件并保存為select.js,剩下的工作我們?nèi)吭谶@個(gè)js中去完成。
函數(shù)名:loadSelect(obj);
參數(shù):select對(duì)象
相關(guān)函數(shù):
程序代碼
function Offset(e)
//取標(biāo)簽的絕對(duì)位置
{
var t = e.offsetTop;
var l = e.offsetLeft;
var w = e.offsetWidth;
var h = e.offsetHeight-2;
while(e=e.offsetParent)
{
t+=e.offsetTop;
l+=e.offsetLeft;
}
return {
top : t,
left : l,
width : w,
height : h
}
}
第一步:把select的絕對(duì)位置記錄下來。一會(huì)替身上來就知道應(yīng)該站那里了。
程序代碼
var offset=Offset(obj);
//這里解釋一下Offset是一個(gè)函數(shù),用來獲取對(duì)象的絕對(duì)位置。寫在loadSelect()函數(shù)外邊的。他有四個(gè)屬性分別是top/left/width/height。
第二步:將select隱藏。
程序代碼
obj.style.display="none";
第三步:虛擬一個(gè)div出來代替select
程序代碼
var iDiv = document.createElement("div");
iDiv.id="selectof" + obj.name;
iDiv.style.position = "absolute";
iDiv.style.width=offset.width + "px";
iDiv.style.height=offset.height + "px";
iDiv.style.top=offset.top + "px";
iDiv.style.left=offset.left + "px";
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
iDiv.style.border="1px solid #3366ff";
iDiv.style.fontSize="12px";
iDiv.style.lineHeight=offset.height + "px";
iDiv.style.textIndent="4px";
document.body.appendChild(iDiv);
第四步:把原始select沒人選中的值給它。
程序代碼
iDiv.innerHTML=obj.options[obj.selectedIndex].innerHTML;
第五步:為替身添加鼠標(biāo)移過樣式。
程序代碼
iDiv.onmouseover=function(){//鼠標(biāo)移到
iDiv.style.background="url(icon_select_focus.gif) no-repeat right 4px";
}
iDiv.onmouseout=function(){//鼠標(biāo)移走
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
}
第六步:添加關(guān)鍵的鼠標(biāo)點(diǎn)擊事件。
程序代碼
iDiv.onclick=function(){//鼠標(biāo)點(diǎn)擊
if (document.getElementById("selectchild" + obj.name)){
//判斷是否創(chuàng)建過div
if (childCreate){
//判斷當(dāng)前的下拉是不是打開狀態(tài),如果是打開的就關(guān)閉掉。是關(guān)閉的就打開。
document.getElementById("selectchild" + obj.name).style.display="none";
childCreate=false;
}else{
document.getElementById("selectchild" + obj.name).style.display="";
childCreate=true;
}
}else{
//初始一個(gè)div放在上一個(gè)div下邊,當(dāng)options的替身。
var cDiv = document.createElement("div");
cDiv.id="selectchild" + obj.name;
cDiv.style.position = "absolute";
cDiv.style.width=offset.width + "px";
cDiv.style.height=obj.options.length *20 + "px";
cDiv.style.top=(offset.top+offset.height+2) + "px";
cDiv.style.left=offset.left + "px";
cDiv.style.background="#f7f7f7";
cDiv.style.border="1px solid silver";
var uUl = document.createElement("ul");
uUl.id="uUlchild" + obj.name;
uUl.style.listStyle="none";
uUl.style.margin="0";
uUl.style.padding="0";
uUl.style.fontSize="12px";
cDiv.appendChild(uUl);
document.body.appendChild(cDiv);
childCreate=true;
for (var i=0;i<obj.options.length;i++){
//將原始的select標(biāo)簽中的options添加到li中
var lLi=document.createElement("li");
lLi.id=obj.options[i].value;
lLi.style.textIndent="4px";
lLi.style.height="20px";
lLi.style.lineHeight="20px";
lLi.innerHTML=obj.options[i].innerHTML;
uUl.appendChild(lLi);
}
var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
for (var j=0;j<obj.options.length;j++){
//為li標(biāo)簽添加鼠標(biāo)事件
liObj[j].onmouseover=function(){
this.style.background="gray";
this.style.color="white";
}
liObj[j].onmouseout=function(){
this.style.background="white";
this.style.color="black";
}
liObj[j].onclick=function(){
//做兩件事情,一是將用戶選擇的保存到原始select標(biāo)簽中,要不做的再好看表單遞交后也獲取不到select的值了。
obj.options.length=0;
obj.options[0]=new Option(this.innerHTML,this.id);
//同時(shí)我們把下拉的關(guān)閉掉。
document.getElementById("selectchild" + obj.name).style.display="none";
childCreate=false;
iDiv.innerHTML=this.innerHTML;
}
}
}
}
最后這個(gè)比較復(fù)雜一點(diǎn),再解釋一下,我們?cè)谧鲞@一步之前,select的樣子已經(jīng)出來了,下一步就是再加一個(gè)div去模仿select被點(diǎn)擊之后出現(xiàn)的下拉選項(xiàng)了。我們可以講select標(biāo)簽的options通過javascript提取出來,把它寫成這樣:
程序代碼
<div>
<ul>
<li>optionName</li>
<li>optionName</li>
<li>optionName</li>
</ul>
</div>
基本上就這樣了。還有些缺陷,有時(shí)間大家可以一起補(bǔ)充!
/************************************
* js模擬Select *
* Author:Daviv *
* Date:2007-4-28 *
* Blog:http://www.iwcn.net *
* Email:jxdawei#163.com *
************************************/
var childCreate=false;
function Offset(e)
//取標(biāo)簽的絕對(duì)位置
{
var t = e.offsetTop;
var l = e.offsetLeft;
var w = e.offsetWidth;
var h = e.offsetHeight-2;
while(e=e.offsetParent)
{
t+=e.offsetTop;
l+=e.offsetLeft;
}
return {
top : t,
left : l,
width : w,
height : h
}
}
function loadSelect(obj){
//第一步:取得Select所在的位置
var offset=Offset(obj);
//第二步:將真的select隱藏
obj.style.display="none";
//第三步:虛擬一個(gè)div出來代替select
var iDiv = document.createElement("div");
iDiv.id="selectof" + obj.name;
iDiv.style.position = "absolute";
iDiv.style.width=offset.width + "px";
iDiv.style.height=offset.height + "px";
iDiv.style.top=offset.top + "px";
iDiv.style.left=offset.left + "px";
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
iDiv.style.border="1px solid #3366ff";
iDiv.style.fontSize="12px";
iDiv.style.lineHeight=offset.height + "px";
iDiv.style.textIndent="4px";
document.body.appendChild(iDiv);
//第四步:將select中默認(rèn)的選項(xiàng)顯示出來
var tValue=obj.options[obj.selectedIndex].innerHTML;
iDiv.innerHTML=tValue;
//第五步:模擬鼠標(biāo)點(diǎn)擊
iDiv.onmouseover=function(){//鼠標(biāo)移到
iDiv.style.background="url(icon_select_focus.gif) no-repeat right 4px";
}
iDiv.onmouseout=function(){//鼠標(biāo)移走
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
}
iDiv.onclick=function(){//鼠標(biāo)點(diǎn)擊
if (document.getElementById("selectchild" + obj.name)){
//判斷是否創(chuàng)建過div
if (childCreate){
//判斷當(dāng)前的下拉是不是打開狀態(tài),如果是打開的就關(guān)閉掉。是關(guān)閉的就打開。
document.getElementById("selectchild" + obj.name).style.display="none";
childCreate=false;
}else{
document.getElementById("selectchild" + obj.name).style.display="";
childCreate=true;
}
}else{
//初始一個(gè)div放在上一個(gè)div下邊,當(dāng)options的替身。
var cDiv = document.createElement("div");
cDiv.id="selectchild" + obj.name;
cDiv.style.position = "absolute";
cDiv.style.width=offset.width + "px";
cDiv.style.height=obj.options.length *20 + "px";
cDiv.style.top=(offset.top+offset.height+2) + "px";
cDiv.style.left=offset.left + "px";
cDiv.style.background="#f7f7f7";
cDiv.style.border="1px solid silver";
var uUl = document.createElement("ul");
uUl.id="uUlchild" + obj.name;
uUl.style.listStyle="none";
uUl.style.margin="0";
uUl.style.padding="0";
uUl.style.fontSize="12px";
cDiv.appendChild(uUl);
document.body.appendChild(cDiv);
childCreate=true;
for (var i=0;i<obj.options.length;i++){
//將原始的select標(biāo)簽中的options添加到li中
var lLi=document.createElement("li");
lLi.id=obj.options[i].value;
lLi.style.textIndent="4px";
lLi.style.height="20px";
lLi.style.lineHeight="20px";
lLi.innerHTML=obj.options[i].innerHTML;
uUl.appendChild(lLi);
}
var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
for (var j=0;j<obj.options.length;j++){
//為li標(biāo)簽添加鼠標(biāo)事件
liObj[j].onmouseover=function(){
this.style.background="gray";
this.style.color="white";
}
liObj[j].onmouseout=function(){
this.style.background="white";
this.style.color="black";
}
liObj[j].onclick=function(){
//做兩件事情,一是將用戶選擇的保存到原始select標(biāo)簽中,要不做的再好看表單遞交后也獲取不到select的值了。
obj.options.length=0;
obj.options[0]=new Option(this.innerHTML,this.id);
//同時(shí)我們把下拉的關(guān)閉掉。
document.getElementById("selectchild" + obj.name).style.display="none";
childCreate=false;
iDiv.innerHTML=this.innerHTML;
}
}
}
}
}
【功能需求】
1、調(diào)用要方便,做好之后應(yīng)該像這樣:
程序代碼
function loadSelect(selectobj){
//傳入一個(gè)select對(duì)象就能將他的樣式美化
}
2、不改變?cè)斜韱雾?xiàng),表單的頁面代碼不去破壞:
程序代碼
<form name="f" onsubmit="getResult();">
<fieldset>
<legend>用戶注冊(cè)</legend>
<div>
<label for="username">帳號(hào)</label>
<input type="text" id="username" name="username" />
</div>
<div>
<label for="pwd">密碼</label>
<input type="password" name="pwd" id="pwd" />
</div>
<div>
<label for="province">省份</label>
<select id="province" name="province">
<option value="10">江西</option>
<option value="11">福建</option>
<option value="12">廣東</option>
<option value="13">浙江</option>
</select>
</div>
</fieldset>
<input type="submit" value="提交" name="btnSub" />
</form>
【實(shí)現(xiàn)思路】
第一步:將表單中的select隱藏起來。
為什么?很簡(jiǎn)單,因?yàn)檫@家伙太頑固了,用css根本搞不出來你想要的。所以把它殺掉。
第二步:用腳本找到select標(biāo)簽在網(wǎng)頁上的絕對(duì)位置。
我們?cè)谀莻€(gè)位置上用DIV標(biāo)簽做個(gè)假的、好看點(diǎn)的來當(dāng)他的替身。
第三步:用腳本把select標(biāo)簽中的值讀出來。
雖然藏起來了,但它里邊的options我們還有用呢,統(tǒng)統(tǒng)取過來。
第四步:當(dāng)用戶點(diǎn)擊select標(biāo)簽的替身,也就是div的時(shí)候。我們?cè)儆靡粋€(gè)div浮在上一個(gè)div的下邊,這個(gè)就是options的替身了。
大致上就是這樣了,接下來我們一步一步去實(shí)現(xiàn)它!
【準(zhǔn)備工作】
1、想好你要把select美化成什么樣子,并準(zhǔn)備好相應(yīng)的圖片。我準(zhǔn)備了兩張小圖,就是下拉箭頭1和下拉箭頭2,1是默認(rèn)樣式,2是鼠標(biāo)移過來的樣式。
2、寫好一個(gè)普通的表單遞交頁面,比如下邊這個(gè)。注意我給select定義了基本的CSS樣式、在頭部添加了調(diào)用js文件的代碼、在body中添加了調(diào)用函數(shù)的腳本。
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
【編寫javascript】
程序代碼
<script type="text/javascript" src="select.js"></script>
新建一個(gè)js文件并保存為select.js,剩下的工作我們?nèi)吭谶@個(gè)js中去完成。
函數(shù)名:loadSelect(obj);
參數(shù):select對(duì)象
相關(guān)函數(shù):
程序代碼
function Offset(e)
//取標(biāo)簽的絕對(duì)位置
{
var t = e.offsetTop;
var l = e.offsetLeft;
var w = e.offsetWidth;
var h = e.offsetHeight-2;
while(e=e.offsetParent)
{
t+=e.offsetTop;
l+=e.offsetLeft;
}
return {
top : t,
left : l,
width : w,
height : h
}
}
第一步:把select的絕對(duì)位置記錄下來。一會(huì)替身上來就知道應(yīng)該站那里了。
程序代碼
var offset=Offset(obj);
//這里解釋一下Offset是一個(gè)函數(shù),用來獲取對(duì)象的絕對(duì)位置。寫在loadSelect()函數(shù)外邊的。他有四個(gè)屬性分別是top/left/width/height。
第二步:將select隱藏。
程序代碼
obj.style.display="none";
第三步:虛擬一個(gè)div出來代替select
程序代碼
var iDiv = document.createElement("div");
iDiv.id="selectof" + obj.name;
iDiv.style.position = "absolute";
iDiv.style.width=offset.width + "px";
iDiv.style.height=offset.height + "px";
iDiv.style.top=offset.top + "px";
iDiv.style.left=offset.left + "px";
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
iDiv.style.border="1px solid #3366ff";
iDiv.style.fontSize="12px";
iDiv.style.lineHeight=offset.height + "px";
iDiv.style.textIndent="4px";
document.body.appendChild(iDiv);
第四步:把原始select沒人選中的值給它。
程序代碼
iDiv.innerHTML=obj.options[obj.selectedIndex].innerHTML;
第五步:為替身添加鼠標(biāo)移過樣式。
程序代碼
iDiv.onmouseover=function(){//鼠標(biāo)移到
iDiv.style.background="url(icon_select_focus.gif) no-repeat right 4px";
}
iDiv.onmouseout=function(){//鼠標(biāo)移走
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
}
第六步:添加關(guān)鍵的鼠標(biāo)點(diǎn)擊事件。
程序代碼
iDiv.onclick=function(){//鼠標(biāo)點(diǎn)擊
if (document.getElementById("selectchild" + obj.name)){
//判斷是否創(chuàng)建過div
if (childCreate){
//判斷當(dāng)前的下拉是不是打開狀態(tài),如果是打開的就關(guān)閉掉。是關(guān)閉的就打開。
document.getElementById("selectchild" + obj.name).style.display="none";
childCreate=false;
}else{
document.getElementById("selectchild" + obj.name).style.display="";
childCreate=true;
}
}else{
//初始一個(gè)div放在上一個(gè)div下邊,當(dāng)options的替身。
var cDiv = document.createElement("div");
cDiv.id="selectchild" + obj.name;
cDiv.style.position = "absolute";
cDiv.style.width=offset.width + "px";
cDiv.style.height=obj.options.length *20 + "px";
cDiv.style.top=(offset.top+offset.height+2) + "px";
cDiv.style.left=offset.left + "px";
cDiv.style.background="#f7f7f7";
cDiv.style.border="1px solid silver";
var uUl = document.createElement("ul");
uUl.id="uUlchild" + obj.name;
uUl.style.listStyle="none";
uUl.style.margin="0";
uUl.style.padding="0";
uUl.style.fontSize="12px";
cDiv.appendChild(uUl);
document.body.appendChild(cDiv);
childCreate=true;
for (var i=0;i<obj.options.length;i++){
//將原始的select標(biāo)簽中的options添加到li中
var lLi=document.createElement("li");
lLi.id=obj.options[i].value;
lLi.style.textIndent="4px";
lLi.style.height="20px";
lLi.style.lineHeight="20px";
lLi.innerHTML=obj.options[i].innerHTML;
uUl.appendChild(lLi);
}
var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
for (var j=0;j<obj.options.length;j++){
//為li標(biāo)簽添加鼠標(biāo)事件
liObj[j].onmouseover=function(){
this.style.background="gray";
this.style.color="white";
}
liObj[j].onmouseout=function(){
this.style.background="white";
this.style.color="black";
}
liObj[j].onclick=function(){
//做兩件事情,一是將用戶選擇的保存到原始select標(biāo)簽中,要不做的再好看表單遞交后也獲取不到select的值了。
obj.options.length=0;
obj.options[0]=new Option(this.innerHTML,this.id);
//同時(shí)我們把下拉的關(guān)閉掉。
document.getElementById("selectchild" + obj.name).style.display="none";
childCreate=false;
iDiv.innerHTML=this.innerHTML;
}
}
}
}
最后這個(gè)比較復(fù)雜一點(diǎn),再解釋一下,我們?cè)谧鲞@一步之前,select的樣子已經(jīng)出來了,下一步就是再加一個(gè)div去模仿select被點(diǎn)擊之后出現(xiàn)的下拉選項(xiàng)了。我們可以講select標(biāo)簽的options通過javascript提取出來,把它寫成這樣:
程序代碼
<div>
<ul>
<li>optionName</li>
<li>optionName</li>
<li>optionName</li>
</ul>
</div>
基本上就這樣了。還有些缺陷,有時(shí)間大家可以一起補(bǔ)充!
復(fù)制代碼 代碼如下:
/************************************
* js模擬Select *
* Author:Daviv *
* Date:2007-4-28 *
* Blog:http://www.iwcn.net *
* Email:jxdawei#163.com *
************************************/
var childCreate=false;
function Offset(e)
//取標(biāo)簽的絕對(duì)位置
{
var t = e.offsetTop;
var l = e.offsetLeft;
var w = e.offsetWidth;
var h = e.offsetHeight-2;
while(e=e.offsetParent)
{
t+=e.offsetTop;
l+=e.offsetLeft;
}
return {
top : t,
left : l,
width : w,
height : h
}
}
function loadSelect(obj){
//第一步:取得Select所在的位置
var offset=Offset(obj);
//第二步:將真的select隱藏
obj.style.display="none";
//第三步:虛擬一個(gè)div出來代替select
var iDiv = document.createElement("div");
iDiv.id="selectof" + obj.name;
iDiv.style.position = "absolute";
iDiv.style.width=offset.width + "px";
iDiv.style.height=offset.height + "px";
iDiv.style.top=offset.top + "px";
iDiv.style.left=offset.left + "px";
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
iDiv.style.border="1px solid #3366ff";
iDiv.style.fontSize="12px";
iDiv.style.lineHeight=offset.height + "px";
iDiv.style.textIndent="4px";
document.body.appendChild(iDiv);
//第四步:將select中默認(rèn)的選項(xiàng)顯示出來
var tValue=obj.options[obj.selectedIndex].innerHTML;
iDiv.innerHTML=tValue;
//第五步:模擬鼠標(biāo)點(diǎn)擊
iDiv.onmouseover=function(){//鼠標(biāo)移到
iDiv.style.background="url(icon_select_focus.gif) no-repeat right 4px";
}
iDiv.onmouseout=function(){//鼠標(biāo)移走
iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
}
iDiv.onclick=function(){//鼠標(biāo)點(diǎn)擊
if (document.getElementById("selectchild" + obj.name)){
//判斷是否創(chuàng)建過div
if (childCreate){
//判斷當(dāng)前的下拉是不是打開狀態(tài),如果是打開的就關(guān)閉掉。是關(guān)閉的就打開。
document.getElementById("selectchild" + obj.name).style.display="none";
childCreate=false;
}else{
document.getElementById("selectchild" + obj.name).style.display="";
childCreate=true;
}
}else{
//初始一個(gè)div放在上一個(gè)div下邊,當(dāng)options的替身。
var cDiv = document.createElement("div");
cDiv.id="selectchild" + obj.name;
cDiv.style.position = "absolute";
cDiv.style.width=offset.width + "px";
cDiv.style.height=obj.options.length *20 + "px";
cDiv.style.top=(offset.top+offset.height+2) + "px";
cDiv.style.left=offset.left + "px";
cDiv.style.background="#f7f7f7";
cDiv.style.border="1px solid silver";
var uUl = document.createElement("ul");
uUl.id="uUlchild" + obj.name;
uUl.style.listStyle="none";
uUl.style.margin="0";
uUl.style.padding="0";
uUl.style.fontSize="12px";
cDiv.appendChild(uUl);
document.body.appendChild(cDiv);
childCreate=true;
for (var i=0;i<obj.options.length;i++){
//將原始的select標(biāo)簽中的options添加到li中
var lLi=document.createElement("li");
lLi.id=obj.options[i].value;
lLi.style.textIndent="4px";
lLi.style.height="20px";
lLi.style.lineHeight="20px";
lLi.innerHTML=obj.options[i].innerHTML;
uUl.appendChild(lLi);
}
var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
for (var j=0;j<obj.options.length;j++){
//為li標(biāo)簽添加鼠標(biāo)事件
liObj[j].onmouseover=function(){
this.style.background="gray";
this.style.color="white";
}
liObj[j].onmouseout=function(){
this.style.background="white";
this.style.color="black";
}
liObj[j].onclick=function(){
//做兩件事情,一是將用戶選擇的保存到原始select標(biāo)簽中,要不做的再好看表單遞交后也獲取不到select的值了。
obj.options.length=0;
obj.options[0]=new Option(this.innerHTML,this.id);
//同時(shí)我們把下拉的關(guān)閉掉。
document.getElementById("selectchild" + obj.name).style.display="none";
childCreate=false;
iDiv.innerHTML=this.innerHTML;
}
}
}
}
}
您可能感興趣的文章:
- 用javascript實(shí)現(xiàn)select的美化的方法
- javascript操作select參考代碼
- javascript Select標(biāo)記中options操作方法集合
- select下拉選擇框美化實(shí)現(xiàn)代碼(js+css+圖片)
- select標(biāo)簽?zāi)M/美化方法采用JS外掛式插件
- select標(biāo)記美化--JS式插件、后期加載
- Js操作Select大全(取值、設(shè)置選中等等)
- 用JavaScript來美化HTML的select標(biāo)簽的下拉列表效果
- js+css實(shí)現(xiàn)select的美化效果
- js自定義select下拉框美化特效
相關(guān)文章
js限制textarea每行輸入字符串長(zhǎng)度的代碼
text通常我們可以直接用maxlength屬性來控制輸入的字符會(huì)長(zhǎng)度2012-10-10js限制文本框?yàn)檎麛?shù)和貨幣的函數(shù)代碼
js限制文本框?yàn)檎麛?shù)和貨幣的函數(shù)代碼,方便一些數(shù)據(jù)庫(kù)字段的控制。2010-10-10用js實(shí)現(xiàn)控制內(nèi)容的向上向下滾動(dòng)效果
用js實(shí)現(xiàn)控制內(nèi)容的向上向下滾動(dòng)效果...2007-06-06通過隱藏option實(shí)現(xiàn)select的聯(lián)動(dòng)效果
開始的時(shí)候需求是根據(jù)一定條件隱藏一部分<option>標(biāo)簽,類似聯(lián)動(dòng)效果,但是目前的html規(guī)范并沒有為<option>提供隱藏的效果,因此常用的設(shè)置display或者visibility無效。2009-11-11