发信人: nightcat() 
整理人: i_am_trueman(2003-10-22 16:31:05), 站内信件
 | 
 
 
自己写一个select
 下面是一个例子,不过我只用IE5测试过,IE4应该也行,其他的浏览器估计
 不行:
 
 test.htm
 
 ------------------------------------------------------------
 
 <HTML>
 <HEAD>
 <TITLE>Cool SelectBox</TITLE>
 <script type="text/javascript" src="select.js"></script>
 <script type="text/javascript">
 <!--
 
 var overOptionCss = "background: buttonhighlight; color: black;\
 					border: 1 inset window;\
 					padding-left: 2; padding-right: 0; padding-bottom: 0; padding-top : 2";
 var sizedBorderCss = "1 outset buttonhighlight";
 
 //-->
 </script>
 
 <style type="text/css">
 .select			{border: 0 inset buttonface; width: 100; font: icon; cursor:  default;}
 .selected		{border: 0 inset buttonface; background: buttonface; paddin g: 0; font: icon;}
 .selectTable	{height: 100%; width: 100%;
 				border: 1 outset buttonhighlight; background: buttonface;}
 .option			{font: icon; padding: 0; padding-left: 3; padding-right: 3;  width: 100%;
 				background: buttonface; border: 1 solid buttonface; padding: 1;}
 .dropDown		{position: absolute; visibility: hidden; width: 100%; z-ind ex: 99;
 				border: 1 outset window; padding: 0;
 				background: buttonface;}
 .select .coolButton	{width: 16px; height: 5; font-family: webdings; pa dding: 1;
 					font-size: 11px;}
 </style>
 
 </HEAD>
 
 <BODY>
 
 <span class="select" id="select1" siZE="1" onchange="alert(this.option s[this.selectedIndex].value)" style="margin: 10; position: relative; t op: -70;">
 	<table class="selectTable" cellspacing="0" cellpadding="0"
 	 onclick="toggleDropDown(this.parentElement)">
 	<tr>
 		<td class="selected"> </td>
 		<td align="CENTER" valign="MIDDLE" class="coolButton">
 		<span style="position: relative; left: 0; top: -2; width: 100%;">6</ span></td>
 	</tr>
 	</table>
 
 	<div class="dropDown" onclick="optionClick()" onmouseover="optionOver ()" onmouseout="optionOut()">
 		<div class="option" value="1" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> Item 1</div>
 		<div class="option" value="2" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> Item 2</div>
 		<div class="option" value="3" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> Item 3</div>
 		<div class="option" value="4" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> Item 4</div>
 		<div class="option" value="5" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> item 5</div>
 		<div class="option" value="6" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> item 6</div>
 		<div class="option" value="7" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> item 7</div>
 
 	</div>
 
 </span>
 
 
 <span class="select" id="select2" siZE="5" onchange="alert('K.')" styl e="position: relative; left: 40;">
 	<table class="selectTable" cellspacing="0" cellpadding="0"
 	 onclick="toggleDropDown(this.parentElement)">
 	<tr>
 		<td class="selected"> </td>
 		<td align="CENTER" valign="MIDDLE" class="coolButton"><span style="p adding-left: 3px; width: 100%;">6</span></td>
 	</tr>
 	</table>
 
 	<div class="dropDown" onclick="optionClick()" onmouseover="optionOver ()" onmouseout="optionOut()">
 		<div class="option" value="1" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> Item 1</div>
 		<div class="option" value="2" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> Item 2</div>
 		<div class="option" value="3" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> Item 3</div>
 		<div class="option" value="4" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> Item 4</div>
 		<div class="option" value="5" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> item 5</div>
 		<div class="option" value="6" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> item 6</div>
 		<div class="option" value="7" nowrap><img src="images/foldericon.gif " width=16 height=16 border=0 align="absmiddle"> item 7</div>
 	</div>
 </span>
 
 
 
 </BODY>
 </HTML>
 
 ----------------------------------------------------------
 
 select.js
 
 ----------------------------------------------------------
 
 var overOptionCss = "background: highlight; color: highlighttext";
 var sizedBorderCss = "2 inset buttonhighlight";
 
 var globalSelect;	//This is used when calling an unnamed selectbox wit h onclick="this.PROPERTY"
 
 var ie4 = (document.all != null);
 
 var q = 0;
 
 
 function initSelectBox(el) {
 	copySelected(el);
 	
 	var size = el.getAttribute("size");
 
 // These two lines combined with execution in optionClick() allow you  to write:
 //		onchange="alert(this.options[this.selectedIndex].value)"
 	el.options = el.children[1].children;
 	el.selectedIndex = findSelected(el);	//Set the index now!
 // Some methods that are supported on the real SELECT box
 	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)");
 // The real select box let you have lot of options with the same NAME.  In that case the item
 // needs two arguments. When using DIVs you can't have two with the sa me NAME (or ID) and
 // then there is no need for the second argument
 	
 	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) {
 //alert("initSized -------->");
 	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; //2 * parseInt(dropdown.style.b orderWidth);
 		}
 
 		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;
 }
 
 // This function returns the first selected option and resets the rest 
 // in case some idiot has set more than one to selcted :-)
 function findSelected(el) {
 	var selected = null;
 
 
 	ec = el.children[1].children;	//the table is the first child
 	var ecl = ec.length;
 	
 	for (var i=0; i<ecl; i++) {
 		if (ec[i].getAttribute("selected") != null) {
 			if (selected == null) {	// Found first selected
 				selected = i;
 			}
 			else
 				ec[i].removeAttribute("selected");	//Like I said. Only one selecte d!
 		}
 	}
 	if (selected == null)
 		selected = 0;	//When starting this is the most logic start value if  none is present
 
 	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) {	// This executes the onchange when  you chnage the option
 			if (selectBox.id != "") {		// For this to work you need to replace  this with an ID or name
 				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.paren tElement.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)) {
 			dropdown = 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;
 }
 
  -- ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.103.124.218]
  | 
 
 
 |