飞龙

导航

关于html中下拉菜单select的样式的改变

首先要告诉大家,如果你是用css的方法,除了箭头部分,其他都可以改变,这是很令人别扭的事,因为其他的样式改了,箭头部分改不了等于无用。
下面举个css改select的例子
<style>
 .box{border:1px solid 

 #C0C0C0;width:182px;height:19px;clip:rect(0px,181px,18px,0px);overflow:hidden;}
 .box2{border:1px solid 

 #F4F4F4;width:180px;height:17px;clip:rect(0px,179px,16px,0px);overflow:hidden;}
 select{position:relative;left:-2px;top:-2px;font-size:12px;width:183px;line-height:14px;bo

 rder:0px;color:#909993;}
 </style>
 <div class=box><div class=box2><select  hidefocus>
 <option selected>网站的封面
 <option value=031113>上海夏天(一一友情提供)
 <option value=031106>上海夏天(一)
 <option value=030916>上海夏天出版了
 <option value=031018>在雨中(二)
 <option value=030915>开往黎明的地铁(二)
 <option value=030913>开往黎明的地铁
 <option value=031029>亲爱的你们在干什么
 <option value=000000>更多封面……
 </select></div></div>
stonesky.gif
如果你想彻底的改变select的样子,有一种方法是用htc,但它有个缺点就是除了ie内核的,其他的他不支持。
css.css
select {font-family: "宋体"; font-size: 12px; BEHAVIOR: url('selectBox.htc'); cursor: hand; }
selectBox.htc
<public:component>
<public:attach event="ondocumentready" handler="initializeSelectBox" />
<public:attach event="onpropertychange" handler="eventChangeProperty" />
<public:attach event="onmousedown" for="document" handler="eventMouseDown" />
<public:attach event="onkeydown" for="document" handler="eventKeyDown" />
<public:property name="setColor" put="setupColor" />
<public:property name="setImage" put="setupImage" />
<public:property name="setDisplayCount" put="setupDisplayCount" />
<public:method name="reInitializeSelectBox" />

<script language="JScript">
var objSelectBox = this;
var widthObject, widthObjectOriginal, heightObject;
var tblTitle, tbdTitle, trTitle, tdTitle;
var objItemWindow, objItemDocument, objItemBody, objItemEvent;
var tblItem, tbdItem, trItem, tdItem;
var leftObject, heightItemWindow, heightTitleTable;
var countMaxItem = 10;
var countItem = this.length;
var is_open = false;
var is_loaded = false;
var focusElement;

var normal_bgcolor = "#3894CF";
                                 //背景颜色--鼠标未经过
var normal_color = "#ffffff";
                                 //文字颜色--鼠标未经过
var active_bgcolor = "#93D3FF";
                                 //背景颜色--鼠标经过      
var active_color = "#ffffff";
                                 //文字颜色--鼠标经过
var normal_border_tag = "1 solid #ffffff";
                                 //列表外边框颜色  1 像素
var active_border_tag = "1 solid #ffffff";
                                 //下拉菜单外边框颜色  1 像素
var font_tag = "normal 12px 宋体";
                                 //字体
var box_h = "20";
                                 //下拉菜单的高度
var arrow_image = "stonesky.gif";
                                 //图片地址 18*16--代替下拉菜单的按钮,向下的三角!
function setupColor(color_list){
    var color_array = color_list.split(",");
    var color = new Array();

    for(i=0; i<color_array.length; i++){
        color[i] = color_array[i];
    }

    if(color[0]) normal_color = color[0];
    if(color[1]) normal_bgcolor = color[1];
    if(color[2]) active_color = color[2];
    if(color[3]) active_bgcolor = color[3];
    if(color[4]) normal_border_tag = "1 solid "+color[4];
    if(color[5]) active_border_tag = "1 solid "+color[5];
}

function setupImage(image_file){
    if(image_file) arrow_image = image_file;
}

function setupDisplayCount(max_count){
    if(max_count) countMaxItem = max_count;
}


function disableScroll(){
    window.execScript("document.onmousewheel = function(){return false;}");
    window.execScript("document.onkeydown = function(){return false;}");
}

function enableScroll(){
    window.execScript("document.onmousewheel = function(){return true;}");
    window.execScript("document.onkeydown = function(){return true;}");
}

//  Mouse Over Event
function eventMouseOverTT(){
 if(!objSelectBox.disabled){
     tblTitle.style.border = active_border_tag;
  imgArrow.style.filter = '';
 }
}

//  Mouse Out Event
function eventMouseOutTT(){
    tblTitle.style.border = normal_border_tag;
//    imgArrow.style.filter = 'gray()';
}

//  Mouse Over Event
function eventMouseOverIT(idx){
    removeItemStyle();
    tdItem[idx].style.color = active_color;
    tdItem[idx].style.background = active_bgcolor;
    focusElement = tdItem[idx];
}

// onMouseDown Event
function eventMouseDown(){
    if(is_open) changeItemWindowDisplay();
}

//  Focus Event
function eventFocusSB(){
 tdTitle_sv.innerHTML = objSelectBox.options[selectedIndex].text;
 tdTitle_sv.style.color = active_color;
 tdTitle_sv.style.background = active_bgcolor;
}

//  Blur Event
function eventBlurSB(){
    tdTitle_sv.style.color = normal_color;
    tdTitle_sv.style.background = normal_bgcolor;
}

//  Key Down Event
function eventKeyDownSB(){
    var keycode = window.event.keyCode ? window.event.keyCode : window.event.which ? window.event.which : window.event.charCode;
    if(is_open && focusElement && keycode == 13){
        nowIndex = focusElement.getAttribute("key");
        changeSelectBoxValue(nowIndex);
    }
}

// onKeyDown
function eventKeyDown(){
    var keycode = window.event.keyCode ? window.event.keyCode : window.event.which ? window.event.which : window.event.charCode;
    var eventElement = window.event.srcElement
    //if(keycode == 32 && eventElement.type == "select-one" && eventElement.name == this.name){
    //    changeItemWindowDisplay();
    //}

    if(is_open && focusElement){
        var firstIndex = 0;
        var lastIndex = countItem - 1;
        var nowIndex = objSelectBox.selectedIndex;
        var tmpIndex = 0;
        var change_value_check = false;
        nowIndex = focusElement.getAttribute("key");

        if(window.event.altKey) closeItemWindow();
        if(keycode == 38){      
            tmpIndex = nowIndex - 1;
            if(tmpIndex < firstIndex) tmpIndex = firstIndex;
            selectIndex = tmpIndex;
            change_value_check = true;
        }else if(keycode == 40){
            tmpIndex = nowIndex + 1;
            if(tmpIndex > lastIndex) tmpIndex = lastIndex;
            selectIndex = tmpIndex;
            change_value_check = true;
        }else if(keycode == 33){ // Page Up
            tmpIndex = nowIndex - countMaxItem - 1;
            if(tmpIndex < firstIndex) tmpIndex = firstIndex;
            selectIndex = tmpIndex;
            change_value_check = true;
        }else if(keycode == 34){ // Page Down
            tmpIndex = nowIndex + countMaxItem - 1;
            if(tmpIndex > lastIndex) tmpIndex = lastIndex;
            selectIndex = tmpIndex;
            change_value_check = true;
        }else if(keycode == 36){ // Home
            selectIndex = firstIndex;
            change_value_check = true;
        }else if(keycode == 35){ // End
            selectIndex = lastIndex;
            change_value_check = true;
        }else if(keycode == 13){
            if(focusElement != null) changeSelectBoxValue(nowIndex);
        }

        if(change_value_check){
            removeItemStyle();
            tdItem[selectIndex].style.color = active_color;
            tdItem[selectIndex].style.background = active_bgcolor;
            objItemBody.scrollTop = selectIndex * 20;
            focusElement = tdItem[selectIndex];
        }
    }
}

// onMouseWheel
// onMouseWheel
function eventMouseWheel(){
    if(is_open){
        if(!focusElement) idx = objSelectBox.selectedIndex;
        else idx = focusElement.key;

        for(i=0; i<window.event.wheelDelta; i+=120) idx--;
        for(i=0; i>window.event.wheelDelta; i-=120) idx++;
        idx = Math.max(idx, 0);
        idx = Math.min(idx, countItem - 1);

        removeItemStyle();
        tdItem[idx].style.color = active_color;
        tdItem[idx].style.background = active_bgcolor;
        objItemBody.scrollTop = idx * 20;
        focusElement = tdItem[idx];
    }
}

// SelectBox Change Property Event
function eventChangeProperty(){
    if(window.event.type == "propertychange" && window.event.propertyName == "selectedIndex"){
        tdTitle_sv.innerHTML = objSelectBox.options[objSelectBox.selectedIndex].text;
        if(objSelectBox.onchange != null) objSelectBox.onchange();
    }
}

function changeItemWindowDisplay(){
    if(is_open == false){
        if(countItem && !objSelectBox.disabled) openItemWindow()
    }else{
        closeItemWindow();
    }
}

function openItemWindow(){
    eventBlurSB();

    heightScreen = window.screen.height;
    spaceDown = heightScreen - window.event.screenY - heightTitleTable;
    spaceUp = heightScreen - spaceDown;

    // Item Window
    if((objSelectBox.length <= countMaxItem && (objSelectBox.length * 20 + 4) > spaceDown) || (objSelectBox.length > countMaxItem && spaceDown < (countMaxItem * 20 + 4))){
        objItemWindow.show(0,(0-heightItemWindow),widthObject,heightItemWindow,tblTitle);
    // Item Window
    }else{
        objItemWindow.show(0,heightTitleTable,widthObject,heightItemWindow,tblTitle);
    }

    var idx = objSelectBox.selectedIndex;
    tdItem[idx].style.color = active_color;
    tdItem[idx].style.background = active_bgcolor;
    objItemBody.scrollTop = idx * 18;
    focusElement = tdItem[idx];
    disableScroll();
    is_open = true;
}

function closeItemWindow(){
    objItemWindow.hide();
    removeItemStyle();
    enableScroll();
    objSelectBox.focus();
    is_open = false;
}

function initializeSelectBox(){
    var browser_version = new Number(((window.navigator.appVersion.split('; '))[1].split(' '))[1]);

    if(this.type != "select-one" || this.size != 0){
        return;
    }else if(navigator.appName != "Microsoft Internet Explorer" || browser_version < 5.5){
        return;
    }else{
        objSelectBox.attachEvent("onfocus",eventFocusSB);
        objSelectBox.attachEvent("onblur",eventBlurSB);

        initializeBasicInformation();
        initializeTitleTable();
        if(countItem){
            initializeItemWindow();
            initializeItemTable();
        }
        is_loaded = true;
    }
}

function reInitializeSelectBox(){
    countItem = objSelectBox.length;
    tblTitle.removeNode(true);
    initializeBasicInformation();
    initializeTitleTable();
    initializeItemWindow();
    initializeItemTable();
}

function initializeBasicInformation(){
    // style.width
    // this.offsetWidth
    //  getStringPixelWidth()
    if(is_loaded == false && objSelectBox.style.width){
        widthObject = parseInt(objSelectBox.style.width);
        widthObjectOriginal = widthObject;
    }else if(is_loaded == false && objSelectBox.offsetWidth){
        widthObject = objSelectBox.offsetWidth;
    }else{
        var lengthMax = 0;
        if(countItem){
            for(i=0; i<countItem; i++){
                lengthItem = getStringLength(objSelectBox.options[i].text);
                if(lengthMax < lengthItem){
                    lengthMax = lengthItem;
                    stringMax = objSelectBox.options[i].text;
                }
            }
            widthObject = getStringPixelWidth(stringMax) + 12 + 12 + 2;
        }
    }
    objSelectBox.style.width = "0px";

    heightTitleTable = box_h;
    if(countItem < countMaxItem){
        heightItemWindow = countItem * 20 + 2 + 2;
        widthItemTable = widthObject - 2;
    }else{
        heightItemWindow = countMaxItem * 20 + 2 + 2;
        widthItemTable = widthObject - 18;
    }
    heightObject = heightTitleTable + heightItemWindow;
}

function initializeTitleTable(){
    if(countItem){
        if(!objSelectBox.selectedIndex) objSelectBox.selectedIndex = 0;
        var textDefault = objSelectBox.options[objSelectBox.selectedIndex].text;
    // 60px
    }else{
        var textDefault = "";
        widthObject=60;
    }
    var tooltip = objSelectBox.tooltip;

    tblTitle = document.createElement("TABLE");
    tblTitle.border = 0;
    tblTitle.cellSpacing = 1;
    tblTitle.cellPadding = 0;
    tblTitle.style.paddingLeft = 2;
    tblTitle.style.width = widthObject+1;//
    tblTitle.style.height = heightTitleTable;
    tblTitle.style.color = normal_color;
    tblTitle.style.background = normal_bgcolor;
    tblTitle.style.border = normal_border_tag;
    tblTitle.style.display = "inline";
    tblTitle.style.verticalAlign = "bottom";
    tblTitle.onmouseover = eventMouseOverTT;
    tblTitle.onmouseout = eventMouseOutTT;
    if(tooltip != null) tblTitle.title = tooltip;

    tbdTitle = document.createElement("TBODY");
    tblTitle.appendChild(tbdTitle);

    trTitle = document.createElement("TR");
    trTitle.onclick = changeItemWindowDisplay;
    tdTitle_sv = document.createElement("TD");
    tdTitle_sv.innerHTML = textDefault;
    tdTitle_sv.width = widthObject - 14 - 4 - 4;
    tdTitle_sv.valign = "absmiddle";
    tdTitle_sv.onselectstart = function(){return false;};
    tdTitle_sv.style.font = font_tag;
 if(!objSelectBox.disabled) tdTitle_sv.style.color = normal_color;
 else tdTitle_sv.style.color = disabled_color;
    tdTitle_sv.style.cursor = "default";
    tdTitle_sv.style.background = normal_bgcolor;
    tdTitle_sv.style.verticalAlign = "text-bottom";
    trTitle.appendChild(tdTitle_sv);
    tdTitle = document.createElement("TD");
    tdTitle.width = "14";
    tdTitle.align = "center";
    tdTitle.onselectstart = function(){return false;};
        imgArrow = document.createElement("IMG");
        imgArrow.src = arrow_image;
        imgArrow.valign = "bottom";
//        imgArrow.style.filter = "gray()";
    tdTitle.appendChild(imgArrow);
    trTitle.appendChild(tdTitle);
    tbdTitle.appendChild(trTitle);

    insertAdjacentElement("afterEnd",tblTitle);
}

function initializeItemWindow(){
    objItemWindow = createPopup();
    objItemDocument = objItemWindow.document;
    objItemBody = objItemDocument.body;
    with(objItemBody.style){
        border = normal_border_tag;
        overflowY = "auto";
        scrollbarFaceColor = "#3894CF";
                                //表面颜色
        scrollbarShadowColor = "#ffffff";
                              //阴影
        scrollbarHighlightColor = "#3894CF";
                                 //高光处
        scrollbar3dlightColor = "#3894CF";
                                //3d
        scrollbarDarkShadowColor = "#3894CF";
                                 //阴影
        scrollbarTrackColor = "#3894CF";
                                // 背景
        scrollbarArrowColor = "#ffffff";
                                //三角
        buttonTextColor = "#ffffff";
                          //
    }
}

function initializeItemTable(){
    tblItem = objItemDocument.createElement("TABLE");
    tblItem.cellSpacing = 2;
    tblItem.cellPadding = 2;
    tblItem.style.width = widthItemTable;
    tblItem.style.color = normal_color;
    tblItem.style.background = normal_bgcolor;

    tbdItem = objItemDocument.createElement("TBODY");
    tblItem.appendChild(tbdItem);

    trItem = new Array();
    tdItem = new Array();
    for(i=0; i<objSelectBox.length; i++){
        textSelectBox = objSelectBox.options[i].text;
        valueSelectBox = objSelectBox.options[i].value;
        var tooltip = objSelectBox.options[i].tooltip;

        trItem[i] = objItemDocument.createElement("TR");
        tdItem[i] = objItemDocument.createElement("TD");
        if(tooltip != null) tdItem[i].title = tooltip;
        tdItem[i].innerHTML = " " + textSelectBox;
        tdItem[i].setAttribute("key",i);
        tdItem[i].height = "16";
        tdItem[i].vAlign = "bottom";
        tdItem[i].onmouseover = function(){eventMouseOverIT(this.getAttribute("key"))}
        tdItem[i].onclick = function(){changeSelectBoxValue(this.getAttribute("key"))}
        tdItem[i].onselectstart = function(){return false;};
  tdItem[i].style.font = font_tag;
  tdItem[i].style.color = normal_color;
  tdItem[i].style.background = normal_bgcolor;
        tdItem[i].style.cursor = "default";
        tdItem[i].style.verticalAlign = "bottom";
        trItem[i].appendChild(tdItem[i]);
        tbdItem.appendChild(trItem[i]);
    }

    objItemBody.insertAdjacentElement("beforeEnd",tblItem);
}

function removeItemStyle(){
    for(i=0; i<countItem; i++){
        tdItem[i].style.color = normal_color;
        tdItem[i].style.background = normal_bgcolor;
    }
}

function changeSelectBoxValue(idx){
    objSelectBox.selectedIndex = idx;
    tdTitle_sv.innerHTML = objSelectBox.options[idx].text;
    closeItemWindow()
}

function getStringLength(string){
    var i, j=0;

    for(i=0;i<string.length;i++) {
        lengthString = escape(string.charAt(i)).length;
        if(lengthString > 4) j++;
        j++;
    }

    return j;
}

function getStringPixelWidth(string_value){
    var ascii_code;
    var string_value_length = string_value.length;
    var character;
    var character_width;
    var character_length;
    var total_width = 0;
    var total_length = 0;

    var special_char_size = 6;
    var multibyte_char_size = 12;
    var base_char_start = 32;
    var base_char_end =  127;
    var ascii_char_size = Array(4,4,4,6,6,10,8,4,5,5,6,6,4,6,4,6,6,6,6,6,6,6,6,6,6,6,4,4,8,6,8,6,12,8,8,9,8,8,7,9,8,3,6,8,7,11,9,9,8,9,8,8,8,8,8,10,8,8,8,6,11,6,6,6,4,7,7,7,7,7,3,7,7,3,3,6,3,11,7,7,7,7,4,7,3,7,6,10,7,7,7,6,6,6,9,6);

    for(i=0; i<string_value_length; i++){
        character = string_value.substring(i,(i+1));
        ascii_code = character.charCodeAt(0);

        if(ascii_code < base_char_start){
            character_width = special_char_size;
        }else if(ascii_code <= base_char_end){
            idx = ascii_code - base_char_start;
            character_width = ascii_char_size[idx];
        }else if(ascii_code > base_char_end){
            character_width = multibyte_char_size;
        }
        total_width += character_width;
    }

    return total_width;
}
</script>
</public:component>

selectbox.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>下拉菜单样式</title>
<link href="css.css" rel="stylesheet" type="text/css">
</head>

<body>
<table height="87" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999">
  <tr>
    <td align="center" valign="middle" bgcolor="#CCCCCC">
     <select name="select">
       <option>下拉菜单样式下拉菜单样式下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
     </select>
     <select name="select">
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
       <option>下拉菜单样式</option>
     </select>
    </td>
  </tr>
</table>
</body>
</html>

第二种方法就是用js,这其实是模拟select的效果,代码如下:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
.table1{background-color:white;FONT-FAMILY: Courier New, Courier;font-size:12px}
.td_out{FONT-FAMILY: Courier New, Courier;font-size:12px;color:#000000;height:15px;border:1 solid #ffffff;}
.td_over{FONT-FAMILY: Courier New, Courier;font-size:12px;cursor:default;background-color:#3366cc;border:1 solid #000000;color:#ffffff;height:15px}
.slv{vertical-align:bottom;FONT-FAMILY: Courier New, Courier;font-size:12px;border-left-width:0;border-top-width:0;border-bottom-width:0;border-right:0 solid #000000;vertical-align:middle;height:18px;color:#000000;}
.down{position:relative;left:-2px;font-size:11px;vertical-align:middle;width:16;height:16;color:#2050b0;background-color:#D0DFF7;border:1 solid #9fA3Ce;writing-mode:tb-rl;font-weight:bold;
}
.seldiv{
position:absolute;z-index:1000;overflow-x:hidden;border-left:1 solid #000000;border-bottom:1 solid #000000;border-right:1 solid #000000;
SCROLLBAR-FACE-COLOR: #d0dff7;
SCROLLBAR-HIGHLIGHT-COLOR: #d0dff7;
SCROLLBAR-SHADOW-COLOR: #FFFFFF;
SCROLLBAR-3DLIGHT-COLOR: #FFFFFF;
SCROLLBAR-ARROW-COLOR: #ffffff;
SCROLLBAR-TRACK-COLOR: #ffffff;
SCROLLBAR-DARKSHADOW-COLOR: #d0dff7;}
</style>
<script language="JavaScript">
document.onclick=hiddenDiv;
function getDivCount() {
 var arr=document.all;
 re=0;
 for (i=0;i<arr.length;i++) {
  str=arr[i].id; 
  if (str.indexOf("ZfDiv_")==0) {  
   re++; 
  }
 }
 return re;
}

function getI(ObjId) {//取得objId的最后一位数字
 for (i=0;i<ObjId.length;i++) {
  if (ObjId.charAt(i)=="_") return ObjId.substr(i+1,ObjId.length-1);
 }
 return 0;
}

function select_edit(TextObj){//鼠标经过高亮度
 TextObj.focus();TextObj.select();
}

function checkValue(ID){  
 var sl=document.all["ZfText_"+ID];
 var sv=document.all["ZfDiv_"+ID];
 var da=document.all["ZfData_"+ID];  
 sv.style.display=''
 
 for(i=0;i<da.rows.length;i++)da.rows[i].style.display=''
 
 for(i=0;i<da.rows.length;i++){
  if(da.rows[i].cells[0].innerText.indexOf(sl.value)!=0)da.rows[i].style.display='none';
  getPosition(ID);
 }
}

function getL(e){
 var l=e.offsetLeft;
 while(e=e.offsetParent){
  l+=e.offsetLeft;
 }
 return l
}
function getT(e){
 var t=e.offsetTop;
 while(e=e.offsetParent){
  t+=e.offsetTop;
 }
 return t
}

function getPosition(ID){
 var sv=document.all["ZfDiv_"+ID];
 var sl=document.all["ZfText_"+ID];
 var spn=document.all["ZfSpan_"+ID];
 var da=document.getElementById("ZfData_"+ID);
 sv.style.pixelWidth=spn.offsetWidth;
 da.style.pixelWidth=sv.offsetWidth;
 sv.style.pixelLeft=getL(spn);
 sv.style.pixelTop=getT(spn)+sl.offsetHeight+3;
 if(da.offsetHeight>200){
  sv.style.pixelHeight=200;
  sv.style.overflowY='scroll';
 }
 else {
  sv.style.pixelHeight=da.offsetHeight;
  sv.style.overflowY='hidden';
 }
}

function dropDown(ID){
 var sv=document.all["ZfDiv_"+ID]
 var tb=document.all["ZfData_"+ID]
 
 if(sv.style.display=='none'){ 
  sv.style.display=''; 
  for(i=0;i<tb.rows.length;i++)tb.rows[i].style.display=''
  getPosition(ID); 
 } else { 
  sv.style.display='none';
 }
}//下拉摸拟层

function hiddenDiv(){
 var o=window.event.srcElement.id;
 var tb
 var sv
 if(o=="") {
  for (j=0;j<getDivCount();j++) {
   tb=document.getElementById('ZfData_'+j);
   sv=document.getElementById('ZfDiv_'+j);  
   for(i=0;i<tb.rows.length;i++) tb.rows[i].style.display='';
   sv.style.display='none';  
  }
 
 }

}//隐藏模拟层


function setValue(obj){
var i=getI(obj.parentElement.parentElement.parentElement.id);
//alert(obj.parentElement.parentElement.parentElement.id);

var sl=document.all["ZfText_"+i];
var sv=document.all['ZfDiv_'+i];
sl.value=obj.innerText;
sv.style.display='none';
//sldIndex=obj.parentElement.rowIndex;
}//给文本框赋值

function over(obj){
 obj.className="td_over"
 obj.title=obj.innerText
 obj.focus();
}//鼠标经过变色

function out(obj){
 obj.className="td_out"
}//鼠标离开还原

function String.prototype.Trim(){return this.replace(/(^\s*)|(\s*$)/g,'')}//自定义去空格函数Trim()


//增加list的接口,ID表示该组控件是页面中的第几个
function add(v,ID){
var sv=document.all['ZfDiv_'+ID];
if(!v.Trim()){return;}
var tb=document.all['ZfData_'+ID];
var c=tb.insertRow(tb.rows.length).insertCell();
c.innerHTML='<nobr>'+v.Trim()+'</nobr>';
c.onmouseover=new Function("over(this)");
c.onmouseout=new Function("out(this)");
c.onclick=new Function("setValue(this)");
c.className="td_out";
v='';
}
//增加inpnubox的接口,在页面中产生一个inputbox控件,下拉列表为空
function addText(name,DefValue) {
 var i=getDivCount();
 document.write('<span id="ZfSpan_'+i+'" style="border:1 solid #9CA0CB">');
 document.write('<input type="text" value="'+DefValue+'" name="'+name+'" id="ZfText_'+i+'" ondblclick="ZfDrop_'+i+'.click()" class="slv" onmouseover="select_edit(this)"  onkeyup="checkValue('+i+')"><input type=button id="ZfDrop_'+i+'" value=">" onclick="this.hideFocus=true;dropDown('+i+');" class="down" onmouseover="this.style.backgroundColor=#EEF3FD" onmouseout="this.style.backgroundColor=\'\'" onmousedown="this.style.backgroundColor=#ABC4F5" onmouseup="this.style.backgroundColor=\'\'"></span>');
 document.write('<div id="ZfDiv_'+i+'" class="seldiv" style="display:none;"><table id="ZfData_'+i+'" onselectstart="return false" border="0"  cellspacing="0" cellpadding="0" class="table1"></table></div>');
}

 

</script>
</head>
<body>

<script language="JavaScript">
addText("name1","a");
add("1234",0);
add("1234",0);
addText("name1","a");
add("1234",1);
add("1234",1);
</script>

 

 

 

 

 

 

 

 

<br><br><br><br><br><br>
<style>
.select{border: 0 inset buttonface; width: 100; font: icon; cursor: default;}
.selected{border: 0 inset buttonface; background: window; padding: 0; font: icon;}
.selectTable{height: 100%; width: 100%;border: 2 inset buttonhighlight; background: buttonface;}
.option    {font: icon; padding: 1; padding-left: 3; padding-right: 3; width: 100%;}
.dropDown{position: absolute; visibility: hidden; width: 100%;border: 1 solid windowtext; padding: 0;background: window;}
.select .button    {width: 16px; height: 5; font-family: webdings; padding: 0;font-size: 11px; border: 2 outset buttonhighlight;}
</style>

<script type="text/javascript">
var overOptionCss = "background: highlight; color: highlighttext";
var sizedBorderCss = "2 inset buttonhighlight";
var globalSelect;
var ie4 = (document.all != null);
var q = 0;
function initSelectBox(el) {
    copySelected(el);
    var size = el.getAttribute("size");
    el.options = el.children[1].children;
    el.selectedIndex = findSelected(el);
    el.remove = new Function("i", "int_remove(this,i)");
    el.item   = new Function("i", "return this.options[i]");
    el.add    = new Function("e", "i", "int_add(this, e, i)");   
    el.options[el.selectedIndex].selected = true;

    dropdown = el.children[1];

    if (size != null) {
        if (size > 1) {
            el.size = size;
            dropdown.style.zIndex = 0;
            initSized(el);
        }
        else {
            el.size = 1;
            dropdown.style.zIndex = 99;
            if (dropdown.offsetHeight > 200) {
                dropdown.style.height = "200";
                dropdown.style.overflow = "auto";
            }
        }
    }
   
    highlightSelected(el,true);
}

function int_remove(el,i) {
    if (el.options[i] != null)
        el.options[i].outerHTML = "";
}

function int_add(el, e, i) {
    var html = "<div class='option' noWrap";
    if (e.value != null)
        html += " value='" + e.value + "'";
    if (e.style.cssText != null)
        html += " style='" + e.style.cssText + "'";
    html += ">";
    if (e.text != null)
        html += e.text;
    html += "</div>"

    if ((i == null) || (i >= el.options.length))
        i = el.options.length-1;
    el.options[i].insertAdjacentHTML("AfterEnd", html);
}
   
function initSized(el) {
    var h = 0;
    el.children[0].style.display = "none";
    dropdown = el.children[1];
    dropdown.style.visibility = "visible";

    if (dropdown.children.length > el.size) {
        dropdown.style.overflow = "auto";
        for (var i=0; i<el.size; i++) {
            h += dropdown.children[i].offsetHeight;
        }

        if (dropdown.style.borderWidth != null) {
            dropdown.style.pixelHeight = h + 4;
        }

        else
            dropdown.style.height = h;
    }

    dropdown.style.border = sizedBorderCss;
    el.style.height = dropdown.style.pixelHeight;
}

function copySelected(el) {
    var selectedIndex = findSelected(el);
    selectedCell = el.children[0].rows[0].cells[0];
    selectedDiv  =     el.children[1].children[selectedIndex];
    selectedCell.innerHTML = selectedDiv.outerHTML;
}

function findSelected(el) {
    var selected = null;


    ec = el.children[1].children;
    var ecl = ec.length;
   
    for (var i=0; i<ecl; i++) {
        if (ec[i].getAttribute("selected") != null) {
            if (selected == null) {
                selected = i;
            }
            else
                ec[i].removeAttribute("selected");
        }
    }
    if (selected == null)
        selected = 0;

    return selected;
}

function toggleDropDown(el) {
    if (el.size == 1) {
        dropDown = el.children[1];
       
        if (dropDown.style.visibility == "")
            dropDown.style.visibility = "hidden";
           
        if (dropDown.style.visibility == "hidden")
            showDropDown(dropDown);
        else
            hideDropDown(dropDown);
    }
}

function optionClick() {
    el = getReal(window.event.srcElement, "className", "option");

    if (el.className == "option") {
        dropdown  = el.parentElement;
        selectBox = dropdown.parentElement;
        oldSelected = dropdown.children[findSelected(selectBox)]

        if(oldSelected != el) {
            oldSelected.removeAttribute("selected");
            el.setAttribute("selected", 1);
            selectBox.selectedIndex = findSelected(selectBox);
        }

        if (selectBox.onchange != null) {
            if (selectBox.id != "") {
                eval(selectBox.onchange.replace(/this/g, selectBox.id));
            }
            else {
                globalSelect = selectBox;
                eval(selectBox.onchange.replace(/this/g, "globalSelect"));
            }
        }

        if (el.backupCss != null)
        el.style.cssText = el.backupCss;
        copySelected(selectBox);
        toggleDropDown(selectBox);
        highlightSelected(selectBox, true);
    }
}

function optionOver() {
    var toEl = getReal(window.event.toElement, "className", "option");
    var fromEl = getReal(window.event.fromElement, "className", "option");
    if (toEl == fromEl) return;
    var el = toEl;
    if (el.className == "option") {
        if (el.backupCss == null)
            el.backupCss = el.style.cssText;
        highlightSelected(el.parentElement.parentElement, false);
        el.style.cssText = el.backupCss + "; " + overOptionCss;
        this.highlighted = true;
    }
}

function optionOut() {
    var toEl = getReal(window.event.toElement, "className", "option");
    var fromEl = getReal(window.event.fromElement, "className", "option");
    if (fromEl == fromEl.parentElement.children[findSelected(fromEl.parentElement.parentElement)]) {
        if (toEl == null)
            return;
        if (toEl.className != "option")
            return;
    }
   
    if (toEl != null) {
        if (toEl.className != "option") {
            if (fromEl.className == "option")
                highlightSelected(fromEl.parentElement.parentElement, true);
        }
    }
   
    if (toEl == fromEl) return;
    var el = fromEl;

    if (el.className == "option") {
        if (el.backupCss != null)
            el.style.cssText = el.backupCss;
    }

}

function highlightSelected(el,add) {
    var selectedIndex = findSelected(el);
    selected = el.children[1].children[selectedIndex];
   
    if (add) {
        if (selected.backupCss == null)
            selected.backupCss = selected.style.cssText;
        selected.style.cssText = selected.backupCss + "; " + overOptionCss;
    }
    else if (!add) {
        if (selected.backupCss != null)
            selected.style.cssText = selected.backupCss;
    }
}

function hideShownDropDowns() {
var el = getReal(window.event.srcElement, "className", "select");
var spans = document.all.tags("SPAN");
var selects = new Array();
var index = 0;
   
    for (var i=0; i<spans.length; i++) {
if ((spans[i].className == "select") && (spans[i] != el)) {
    ropdown = spans[i].children[1];
if ((spans[i].size == 1) && (dropdown.style.visibility == "visible"))
    selects[index++] = dropdown;
    }
}
for (var j=0; j<selects.length; j++) {
hideDropDown(selects[j]);
}
}

function hideDropDown(el) {
    if (typeof(fade) == "function")
    fade(el, false);
    else
    el.style.visibility = "hidden";
}

function showDropDown(el) {
    if (typeof(fade) == "function")
    fade(el, true);
    else if (typeof(swipe) == "function")
    swipe(el, 2);
    else
    el.style.visibility = "visible";
}

function initSelectBoxes() {
var spans = document.all.tags("SPAN");
var selects = new Array();
var index = 0;

for (var i=0; i<spans.length; i++) {
if (spans[i].className == "select")
selects[index++] = spans[i];
}
for (var j=0; j<selects.length; j++) {
initSelectBox(selects[j]);
}   
}

function getReal(el, type, value) {
temp = el;
while ((temp != null) && (temp.tagName != "BODY")) {
if (eval("temp." + type) == value) {
el = temp;
return el;
}
temp = temp.parentElement;
}
return el;
}
if (ie4) {
window.onload = initSelectBoxes;
document.onclick = hideShownDropDowns;
}
function writeSelectBox(matrix, id, size, onchange, css) {
    var d = window.document;
    var ie4 = (document.all != null);
    if (ie4) {
//alert("Before!");
var s = createIEString(matrix, id, size, onchange, css);
document.write(s);
//alert("After!");
}else
{
document.write(createXString(matrix, id, size, onchange, css));}
}

function createIEString(matrix, id, size, onchange, css) {
    var str = "";
        str += '<span class="select"';
        if (size == null)
            size = 1;
        str += ' size="' + size + '"';   
        if (id != null)
            str += ' id="' + id + '"';
        if (onchange != null)
            str += ' onchange="' + onchange + '"';
        if (css != null)
        str += ' style="' + css + '"';
        str += '>\n';
        str += '<table class="selectTable" cellspacing="0" cellpadding="0"\n';
        str += ' onclick="toggleDropDown(this.parentElement)">\n';
        str += '<tr>\n';
        str += '<td class="selected"> </td>\n';
        str += '<td align="CENTER" valign="MIDDLE" class="Button"\n';
        str += ' onmousedown="this.style.border=\'2 inset buttonhighlight\'"\n';
        str += ' onmouseup="this.style.border=\'2 outset buttonhighlight\'">\n';
        str += '<span style="position: relative; left: 0; top: -2; width: 100%;">6</span></td>\n';
        str += '</tr>\n';
        str += '</table>\n';
        str += '<div class="dropDown" onclick="optionClick()" onmouseover="optionOver()" onmouseout="optionOut()">\n';
       
        for (var i=0; i<matrix.length; i++) {
            html     = matrix[i].html;
            value    = matrix[i].value;
            css      = matrix[i].css;
            selected = matrix[i].selected;
            str += '<div class="option"';
            if (value != null)
                str += ' value="' + value + '"';
            if (css != null)
                str += ' style="' + css + '"';
            if (selected != null)
                str += ' selected';
            str += '>\n';
            str += html;
            str += '</div>\n';
        }
        str += '</div>\n';
        str += '</span>\n';
    return str;
}

function createXString(matrix, id, size, onchange, css) {
var str = '<form>\n';
str += '<select';
if (size == null)
    size = 1;
str += ' size="' + size + '"';   
if (id != null)
    str += ' id="' + id + '"';
if (onchange != null)
    str += ' onchange="' + onchange + '"';
str += '>\n';

for (var i=0; i<matrix.length; i++) {
    html     = matrix[i].html;
    value    = matrix[i].value;
    css      = matrix[i].css;
    selected = matrix[i].selected;
    str += '\n<option';
    if (value != null)
    str += ' value="' + value + '"';
    if (selected != null)
        str += ' selected';
    str += '>';
    str += stripTags(html);
    str += '</option>\n';
    }
    str += '\n</select>\n';
    str += '</form>\n';
return str;
}

function stripTags(str) {
    var s = 0;
    var e = -1;
    var r = "";
    s = str.indexOf("<",e);   
    do {
        r += str.substring(e + 1,s);
        e = str.indexOf(">",s);
        s = str.indexOf("<",e);
    }
    while ((s != -1) && (e != -1))
    r += str.substring(e + 1,str.length);
    return r;
}

function Option(html, value, css, selected) {
    this.html = html;
    this.value = value;
    this.css = css;
    this.selected = selected;
}
</script>
<script type="text/javascript">
var optionArray = new Array();
optionArray[0] = new Option("Item 1", "value 1", "color: blue; text-decoration: underline;");
optionArray[1] = new Option("Item <b>2</b>", "value 2");
optionArray[2] = new Option("Item 3", "value 3", "color: red;", "selected");
optionArray[3] = new Option("Item 4", "value 4");
optionArray[4] = new Option("Item 5", "value 5");
writeSelectBox(optionArray, "select1", 1, "alert(this.options[this.selectedIndex].value)", "margin-left: 10;");
</script>
<br>
<script type="text/javascript">
writeSelectBox(optionArray, "select3", 3, "alert(this.options[this.selectedIndex].value)", "margin-left: 10;");
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
.n1 {
 border-right: 1px none #999999;
 border-top: 1px solid #999999;
 border-bottom: 1px solid #DBDBDB;
 border-left: 1px solid #999999;
 cursor: default;
 width:80px
}
.n2 {
 background: url(<a href="http://www.blueidea.com/bbs/images/newwin.gif" target="_blank">http://www.blueidea.com/bbs/images/newwin.gif</a>) no-repeat center center;
 border-top: 1px solid #999999;
 border-right: 1px solid #999999;
 border-bottom: 1px solid #DBDBDB;
 border-left: 1px none;
 width: 18px;
 cursor: default;
}
.ss {
 color: #FFFFFF;
 background: #0A246A;
}
-->
</style>

 

 

 

 

 

<STYLE>
BODY {
SCROLLBAR-FACE-COLOR: #d80000;
 SCROLLBAR-HIGHLIGHT-COLOR: #0000fc;
 SCROLLBAR-SHADOW-COLOR: #24fc90;
 SCROLLBAR-ARROW-COLOR: #fcfc48;
 SCROLLBAR-TRACK-COLOR: #002400;
 SCROLLBAR-DARKSHADOW-COLOR: #00b448;
 SCROLLBAR-BASE-COLOR: #fc0000
}
</STYLE>
<script  lanuage="JScript">
function  turnit(ss)
{

  if  (ss.style.display=="none") 
    {ss.style.display="";
}

  else
    {ss.style.display="none";  
   }
}
function sele(tid)
{
bb.style.display='none';
text1.value=tid.innerText;
}
function over(tid)
{
for (var i=1;i<4;i++)
{
eval('t'+i).className=''
}
tid.className='ss'
}
function out(tid)
{
if (bb.style.display!='none')
tid.className=''
}
</script>
<style type="text/css">
<!--
table {
 font-size: 9pt;
}
-->
</style>
</head>

<body>
<table width="98" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="98" nowrap>
<input name="text1" type="text" size="10" class=n1 readonly value=请选择 onclick=turnit(bb)><input name="Submit" type="text" class="n2" value="" readonly onclick=turnit(bb)>
     
    </td>
  </tr>
  <tr>
    <td id=bb style=display:none><div id="Layer1" style="position:absolute; width:98px; height:100px; overflow: scroll; overflow-x:hidden; z-index: 1;" class="n1" >
        <table width="100%" border="0" cellpadding="0" cellspacing="0" id=tb>
          <tr>
            <td  id=t1 onMouseOver =over(this) onMouseOut =out(this) onclick=sele(this)>选择1</td>
          </tr>
          <tr>
            <td id=t2 onMouseOver =over(this) onMouseOut =out(this) onclick=sele(this)>选择2</td>
          </tr>
          <tr>
            <td id=t3 onMouseOver =over(this) onMouseOut =out(this) onclick=sele(this)>选择3</td>
          </tr>
        
        </table>
      </div></td>
  </tr>
</table>
</body>
</html>


 

posted on 2006-05-07 13:26  飞龙  阅读(30834)  评论(0编辑  收藏  举报