本文旨在大家对select和模拟select有一个全面地了解,使此项技术能发挥出更高的水平。
最初的select的确很平淡,就是一个下拉选项列表:
运行代码框
<style>
select,option {background-color:lime}
</style>
<select>
<center>
<option>1
<option>2
<option>不是一样的嘛</select>
<style>
select,option {background-color:lime;font-family:华文行楷;color:red;}
</style>
<select name="select" onfocus="this.options[0].innerText='上海'">
<option value="上海">上海</option>
<option value="北京">北京</option>
<option value="香港">香港</option>
</select>
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
日久天长之后,人们对select要求高了,要修一修边幅:
运行代码框
<br><br>
<script>
i=1;
</script>
<center>
<form name="form1">
<select name="select01">
<option>11111 111111 111111111111</option>
<option>2222222</option>
<option>333333</option>
<option>4444444</option>
</select>
<span style="position:relative;left:-23px;width:17px;height:17px;font:7px 'wingdings 3';color:#cc0000;background:cyan;border:2px outset;text-align:center;padding-top:3px;cursor:default" onclick="if(i>=document.form1.select01.options.length){i=0}document.form1.select01.options[i].selected=true;i++">q
<iframe src="" style="position:absolute;top:0;left:0;z-index:-1; width:15px;height:15px;filter:alpha(opacity=0))" frameborder="0"></iframe>
</span>
</form>
<br><br><br>
<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>
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
要加一些链接,变成菜单:
运行代码框
<STYLE>
body { cursor: url(http://vip.6to23.com/candylau/image/xiucursor.cur)}
</STYLE>
<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 = "";
}
f