为Html 的Select 加一个提示语和输入方法

xieyj(原作)

  1<html>
  2<head>
  3<script language="JavaScript">   
  4<!--   
  5//定义 select 原值   
  6var oldValue,oldText;   
  7//select下拉框的onkeydown事件,修改下拉框的值   
  8function catch_keydown(sel)   
  9{   
 10switch(event.keyCode)   
 11{   
 12case 13: //回车键   
 13event.returnValue = false;   
 14break;   
 15case 27: //Esc键   
 16sel.options[sel.selectedIndex].text = oldText;   
 17sel.options[sel.selectedIndex].value = oldValue;   
 18event.returnValue = false;   
 19break;   
 20case 8: //空格健   
 21var s = sel.options[sel.selectedIndex].text;   
 22s = s.substr(0,s.length-1);   
 23if (sel.options[sel.selectedIndex].value==sel.options[sel.selectedIndex].text)   
 24{   
 25sel.options[sel.selectedIndex].value=s;   
 26sel.options[sel.selectedIndex].text=s;   
 27}   
 28event.returnValue = false;   
 29break;   
 30}   
 31if (!event.returnValue && sel.onchange)   
 32sel.onchange(sel)   
 33} 
 34
 35//select下拉框的onkeypress事件,修改下拉框的值   
 36function catch_press(sel){   
 37if(sel.selectedIndex>=0){   
 38var s = sel.options[sel.selectedIndex].text + String.fromCharCode(event.keyCode);   
 39if (sel.options[sel.selectedIndex].value==sel.options[sel.selectedIndex].text)   
 40{   
 41sel.options[sel.selectedIndex].value=s;   
 42sel.options[sel.selectedIndex].text=s;   
 43}   
 44event.returnValue = false;   
 45if (!event.returnValue && sel.onchange)   
 46sel.onchange(sel)   
 47}   
 48} 
 49
 50//select下拉框的onfocus事件,保存下拉框原来的值   
 51function catch_focus(sel) {   
 52oldText = sel.options[sel.selectedIndex].value;   
 53oldValue = sel.options[sel.selectedIndex].value;   
 54} 
 55
 56//恢复select下拉列表当前选中的值   
 57function LoadSelect(obj,value)   
 58{   
 59for (var i=0; i< obj.options.length; i++)   
 60if (obj.options[i].value == value)   
 61{   
 62obj.selectedIndex = i;   
 63break;   
 64}   
 65} 
 66
 67//select 选择框鼠标上移时提示选择的内容   
 68function selMouseOver(obj)   
 69{   
 70with (document.all.div_hint)   
 71{   
 72innerText = obj.options[obj.selectedIndex].text;   
 73if (innerText.length > 0)   
 74{   
 75innerText = " " + innerText + " ";   
 76style.display = "block";   
 77style.left = event.clientX + 16;   
 78style.top = event.clientY;   
 79}   
 80}   
 81} 
 82
 83//select 选择框鼠标移开时消失   
 84function selMouseOut(obj)   
 85{   
 86with (document.all.div_hint)   
 87{   
 88style.display = "none"   
 89}   
 90}   
 91//-->   
 92</script>
 93</head>
 94<body>
 95<!--调用-->
 96<select name="tmpSel" onfocus="catch_focus(this)" onkeydown="catch_keydown(this)" onkeypress="catch_press(this)" onmouseout="selMouseOut(this)" onmouseover="selMouseOver(this)" style="width:130px;z-index:-1">
 97<option value=""></option>
 98</select>
 99<!--提示块-->
100<div id="div_hint" style="font-size:12px;color:red;display:none;position:absolute; z-index:2; top:200;background-color: #F7F7F7; layer-background-color: #0099FF; border: 1px #9c9c9c solid;filter:Alpha(style=0,opacity=80,finishOpacity=100);"></div>
101</body>
102</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus