js美化selectドロップダウンリスト

10911 ワード

ここ数日、会社はselectドロップダウンリストを美化する必要があります.これを記録して後で使用します.js部分はもうオブジェクトにカプセル化されています.htmlページnewでラベル付きのname値をつけるだけで使用できます.便利ではありませんか.selectのスタイルの大きさの画像の背景はすべて自由に修正することができて、cssのスタイルを修正するだけでいいです!
コードを貼り始めました.
html:

<!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" />
<link href="css/base.css" rel="stylesheet" type="text/css"/>
<link href="css/importselect.css" rel="stylesheet" type="text/css"/>
<title>select dom</title>
<script type="text/javascript" src="js/importselect.js"></script>
<script type="text/javascript">
    window.onload=function (){
        new selectStyle('area_selet2');
        new selectStyle('area_selet1');
    };
</script>
</head>
<body>
    <div class="center">
        <div id="age_sel_2">
            <select name="area_selet2">
                <option value="   ">   </option>
                <option value="ASP" >ASP</option>
                <option value="PHP" >PHP</option>
                <option value=".NET" >.NET</option>
            </select>
        </div>
    </div>
    <div class="center">
        <div id="age_sel_2">
            <select name="area_selet1">
                <option value="   ">   </option>
                <option value="ASP" >ASP</option>
                <option value="PHP" >PHP</option>
                <option value=".NET" >.NET</option>
            </select>
        </div>
    </div>
</body>
</html>

次はcssスタイルセクションです.

html{background:#fff;}
.center{ margin-left:auto; margin-right:auto; margin-top:10px; width:300px;position:relative;}
#age_sel_2 div.tag_select{display:block;width:141px;height:36px;line-height:36px; float:left; color:#fff; text-align:center; font-size:12px;}
#age_sel_2 div.tag_select_hover{display:block;width:141px;height:36px;line-height:36px;float:left; color:#fff; text-align:center; font-size:12px;}
#age_sel_2 div.tag_select_open{display:block;color:#fff;width:141px;height:36px;line-height:36px;text-align:center;font-size:12px;float:left;}
#age_sel_2 ul.tag_options{width:136px;font-size:12px;color:#fff;line-height:36px;text-align:center;float:left;border:1px solid #37A2DA;border-top:0;background:#39A9FE;position:absolute;left:43px; top:33px;}
#age_sel_2 ul.tag_options li{display:block;width:136px;height:36px;text-decoration:none;}
#age_sel_2 ul.tag_options li.open_hover{background:#2174B7;color:#fff;}
#age_sel_2 ul.tag_options li.open_selected{background:#B2DBFC;color:#000;}
.select_box{background:url(../images/select02.gif) no-repeat;width:181px; height:36px;}
.select_txt{width:40px; height:36px;font-size:12px;color:#fff;line-height:36px;text-align:center;float:left;}
.select_fh{background:url(../images/select01.png) -17px 0 no-repeat;width:13px;height:16px;position:absolute;left:155px;top:11px;}
.select_fh_on{background:url(../images/select01.png) 0 0 no-repeat;width:16px;height:14px;position:absolute;left:155px;top:11px;}

次はjsセクションです.

function selectStyle(name){
    this.selects = document.getElementsByName(name);
    isIE = (document.all && window.ActiveXObject && !window.opera) ? true : false;
    this.rSelects();
};
     
selectStyle.prototype.stopBubbling=function (ev) { 
    ev.stopPropagation()
};
//  $   id  
function $(id) {
    return document.getElementById(id)
};
//  dom  
 selectStyle.prototype.rSelects=function () {
    for (i=0;i<this.selects.length;i++){
        this.selects[i].style.display = 'none';
        select_tag = document.createElement('div');
        select_tag.id = 'select_' + this.selects[i].name;
        select_tag.className = 'select_box';
        this.selects[i].parentNode.insertBefore(select_tag,this.selects[i]);
     
        select_text = document.createElement('div'); 
        select_text.className='select_txt';
        select_text.innerHTML='  ';
        select_tag.appendChild(select_text);
        select_fh = document.createElement('div'); 
        select_fh.className='select_fh';
        select_fh.id='select_fh';
        select_tag.appendChild(select_fh);
     
        select_info = document.createElement('div'); 
        select_info.id = 'select_info_' + this.selects[i].name;
        select_info.className='tag_select';
        select_info.style.cursor='pointer';
        select_tag.appendChild(select_info);
        select_ul = document.createElement('ul'); 
        select_ul.id = 'options_' + this.selects[i].name;
        select_ul.className = 'tag_options';
        select_ul.style.position='absolute';
        select_ul.style.display='none';
        select_ul.style.zIndex='999';
        select_tag.appendChild(select_ul);
        this.rOptions(i,this.selects[i].name);
        this.mouseSelects(this.selects[i].name);
        if (isIE){
            this.selects[i].onclick = new Function("clickLabels3('"+this.selects[i].name+"');window.event.cancelBubble = true;");
        }
        else if(!isIE){
            this.selects[i].onclick = new Function("clickLabels3('"+this.selects[i].name+"')");
            this.selects[i].addEventListener("click", this.stopBubbling, false);
        }  
    }
}
 selectStyle.prototype.rOptions=function (i, name) {
    var _this=this;
    var options = this.selects[i].getElementsByTagName('option');
    var options_ul = 'options_' + name;
    for (n=0;n<this.selects[i].options.length;n++){ 
        option_li = document.createElement('li');
        option_li.style.cursor='pointer';
        option_li.index=n;
        option_li.className='open';
        $(options_ul).appendChild(option_li);
        option_text = document.createTextNode(this.selects[i].options[n].text);
        option_li.appendChild(option_text);
        option_selected = this.selects[i].options[n].selected;
        if(option_selected){
            option_li.className='open_selected';
            option_li.id='selected_' + name;
            $('select_info_' + name).appendChild(document.createTextNode(option_li.innerHTML));
        }
        option_li.onmouseover = function(){ this.className='open_hover';}
        option_li.onmouseout = function(){
            if(this.id=='selected_' + name){
                this.className='open_selected';
            }
            else {
                this.className='open';
            }
        } 
        option_li.onclick =function(){_this.clickOptions(i,this.index,_this.selects[i].name)};//new Function("this.clickOptions("+i+","+n+",'"+this.selects[i].name+"')");
    }
}
     
 selectStyle.prototype.mouseSelects=function (name){
    var _this=this;
    var sincn = 'select_info_' + name;
    if (isIE){
        $(sincn).onclick = function(){_this.clickSelects(name);window.event.cancelBubble=true;};// new Function("_this.clickSelects('"+name+"');window.event.cancelBubble = true;");
    }
    else if(!isIE){
        $(sincn).onclick = function(){_this.clickSelects(name)};
        $('select_info_' +name).addEventListener("click", this.stopBubbling, false);
    }
}
//        
 selectStyle.prototype.clickSelects=function (name){
    var sincn = 'select_info_' + name;
    var sinul = 'options_' + name; 
    for (i=0;i<this.selects.length;i++){ 
        if(this.selects[i].name == name){    
            if( $(sincn).className =='tag_select'){
                $(sincn).className ='tag_select_open';
                $(sinul).style.display = '';
            }
            else if( $(sincn).className =='tag_select_open'){
                $(sincn).className = 'tag_select';
                $(sinul).style.display = 'none';
            }
        }
        else{
            $('select_info_' + this.selects[i].name).className = 'tag_select';
            $('options_' + this.selects[i].name).style.display = 'none';
        }
    }
}
//      
 selectStyle.prototype.clickOptions=function (i,n, name){
    var li = $('options_' + name).getElementsByTagName('li');
    $('selected_' + name).className='open';
    $('selected_' + name).id='';
    li[n].id='selected_' + name;
    li[n].className='open_hover';
    $('select_' + name).removeChild($('select_info_' + name));
    select_info = document.createElement('div');
    select_info.id = 'select_info_' + name;
    select_info.className='tag_select';
    select_info.style.cursor='pointer';
    $('options_' + name).parentNode.insertBefore(select_info,$('options_' + name));
    this.mouseSelects(name);
    $('select_info_' + name).appendChild(document.createTextNode(li[n].innerHTML));
    $( 'options_' + name ).style.display = 'none' ;
    $( 'select_info_' + name ).className = 'tag_select';
    this.selects[i].options[n].selected = 'selected';
}