自動提示文本框首先離不開文本框<input type="text">自己、而提示框則採用<div>塊內嵌項目列表<ul>來實現。當前用戶在文本框中每輸入一個字符(onkeyup事件)時就在預約的"顏色名稱集(功能很小很小)"中查找、找到匹配的項就動態加載到<ul>中、顯示給用戶選擇、HTML代碼以下:javascript
- <body>
- <form method="post" name="myForm1">
- Color:<input type="text" name="colors" id="colors" onkeyup="findColors();" />
- </form>
- <div id="popup">
- <ul id="colors_ul"></ul>
- </div>
- </body>
考慮到<div>塊的位置必須出如今輸入框的下面、所以採用css的絕對定位、並設置兩個邊框屬性、一個用於有匹配結果時顯示提示框<div>、另外一個用於未查到匹配結果時隱藏提示框。相應的頁面設置和表單的css樣式以下:css
- <style>
- <!--
- body{
- font-family: Arial,Helvetica,sans-serif;
- font-size: 12px; padding: 0px; margin: 5px;
- }
- form{padding: 0px; margin: 0px;}
- input{
- /*用戶輸入框的樣式*/
- font-family: Arial,Helvetica,sans-serif;
- font-size: 12px; border: 1px solid #000000;
- width: 200px; padding: 1px; margin: 0px;
- }
- #popup{
- /*提示框div塊的樣式*/
- position: absolute; width: 202px;
- color: #004a7e; font-size: 12px;
- font-family: Arial,Helvetica,sans-serif;
- left: 36px; top: 25px;
- }
- #popup.show{
- /*顯示提示框的邊框*/
- border: 1px solid #004a7e;
- }
- #popup.hide{
- /*隱藏提示框的邊框*/
- border: none;
- }
- -->
- </style>
當用戶在文本框中輸入任意一個字符時、則在預約好的"顏色名稱集"中搜索。若是找到匹配的項則存在一個數組中、並傳遞給顯示提示框的函數setColors()、不然利用函數clearColors()清除提示框、代碼以下:html
- var oInputField;
- var oPopDiv;
- var oColorsUl;
- var aColors = ["red","green","blue","magenta","yellow","chocolate","black" +
- "","darkcyan","darkgreen","darkhaki","ivory","darkmagenta","cornfloewrblue" +
- "","bcksa","cgsa","rdgdsa","hfdsa","rqga","bhfdag","cadgdsa"];
- aColors.sort(); //按字母排序
- function initVars(){
- //初始化變量
- oInputField = document.forms["myForm1"].colors;
- oPopDiv = document.getElementById("popup");
- oColorsUl = document.getElementById("colors_ul");
- }
- function findColors(){
- initVars(); //初始化變量
- if(oInputField.value.length > 0){
- var aResult = new Array(); //用於存放匹配結果
- for(var i = 0 ; i < aColors.length ; i++ ){
- //必須是從單詞的開始處匹配
- if(aColors[i].indexOf(oInputField.value) == 0)
- aResult.push(aColors[i]); //加入結果
- }
- if(aResult.length > 0) //若是有匹配的顏色則顯示出來
- setColors(aResult);
- else //不然就清除、用戶多輸入一個字母
- clearColors(); //就有可能從有匹配到無、到無的時候須要清除
- }
- else
- clearColors(); //無輸入時清除提示框
- }
所謂的"顏色名稱集合"就是一個特定的數組aColors、裏面存放了不少預約好的顏色名稱、實際運用中這個數組應該是服務器上的動態數據、明天來加上、用Ajax方式把後臺數據查詢出來、付給這個數組。其實很簡單、然而數據庫怎麼存儲、跟查詢時sql語句的拼寫、就很重要了、不過我基本上一點也不瞭解這方面的東西!java
setColors()和clearColors()分別用來顯示和清除提示框、用戶每輸入一個字符就調用一次findColors()函數、找到匹配項時調用setColors()、不然調用clearColors()。node
傳遞給setColors()的參數是一個數組、裏面存放着全部匹配用戶輸入的數據、所以setColors()的職責就是將這些匹配項一個個放入<li>中、並添加到<ul>裏、而clearColors()則直接清除整個提示框便可。代碼以下:sql
在給提示框中的每一項<li>添加鼠標事件、鼠標通過時高亮顯示、單擊鼠標時則自動將選項賦給輸入框、並清空提示框。<ul>的css樣式風格以下:
- function clearColors(){
- //清除提示內容
- for(var i = oColorsUl.childNodes.length - 1 ; i >= 0 ; i-- )
- oColorsUl.removeChild(oColorsUl.childNodes[i]);
- oPopDiv.className = "hide" ;
- }
- function setColors(the_colors){
- //顯示提示框、傳入的參數即爲匹配出來的結果組成的數組
- clearColors(); //沒輸入一個字母就先清楚原先的提示、再繼續
- oPopDiv.className = "show" ;
- var oLi ;
- for(var i = 0 ; i < the_colors.length ; i++ ){
- //將匹配的提示結果逐一顯示給用戶
- oLi = document.createElement("li");
- oColorsUl.appendChild(oLi);
- oLi.appendChild(document.createTextNode(the_colors[i]));
- oLi.onmouseover = function(){
- this.className = "mouseOver" ; //鼠標指針通過時高亮
- }
- oLi.onmouseout = function(){
- this.className = "mouseOut" ; //鼠標指針離開時恢復原樣
- }
- oLi.onclick = function(){
- //用戶單擊某個匹配項時、設置輸入框爲該項的值
- oInputField.value = this.firstChild.nodeValue;
- clearColors(); //同時清除提示框
- }
- }
- }
- <style>
- <!--
- /*提示框的樣式風格*/
- ul{
- list-style: none;
- margin: 0px; padding: 0px;
- }
- li.mouseOver{
- background-color: #004a7e;
- color: #FFFFFF;
- }
- li.mouseOut{
- background-color: #FFFFFF;
- color: #004a7e;
- }
- -->
- </style>
圖是運行效果、IE8跟火狐都行:數據庫
完整代碼以下:數組
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>匹配用戶輸入</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <style>
- <!--
- body{
- font-family: Arial,Helvetica,sans-serif;
- font-size: 12px; padding: 0px; margin: 5px;
- }
- form{padding: 0px; margin: 0px;}
- input{
- /*用戶輸入框的樣式*/
- font-family: Arial,Helvetica,sans-serif;
- font-size: 12px; border: 1px solid #000000;
- width: 200px; padding: 1px; margin: 0px;
- }
- #popup{
- /*提示框div塊的樣式*/
- position: absolute; width: 202px;
- color: #004a7e; font-size: 12px;
- font-family: Arial,Helvetica,sans-serif;
- left: 36px; top: 25px;
- }
- #popup.show{
- /*顯示提示框的邊框*/
- border: 1px solid #004a7e;
- }
- #popup.hide{
- /*隱藏提示框的邊框*/
- border: none;
- }
- /*提示框的樣式風格*/
- ul{
- list-style: none;
- margin: 0px; padding: 0px;
- }
- li.mouseOver{
- background-color: #004a7e;
- color: #FFFFFF;
- }
- li.mouseOut{
- background-color: #FFFFFF;
- color: #004a7e;
- }
- -->
- </style>
- <script type="text/javascript">
- var oInputField;
- var oPopDiv;
- var oColorsUl;
- var aColors = ["red","green","blue","magenta","yellow","chocolate","black" +
- "","darkcyan","darkgreen","darkhaki","ivory","darkmagenta","cornfloewrblue" +
- "","bcksa","cgsa","rdgdsa","hfdsa","rqga","bhfdag","cadgdsa"];
- aColors.sort(); //按字母排序
- function initVars(){
- //初始化變量
- oInputField = document.forms["myForm1"].colors;
- oPopDiv = document.getElementById("popup");
- oColorsUl = document.getElementById("colors_ul");
- }
- function findColors(){
- initVars(); //初始化變量
- if(oInputField.value.length > 0){
- var aResult = new Array(); //用於存放匹配結果
- for(var i = 0 ; i < aColors.length ; i++ ){
- //必須是從單詞的開始處匹配
- if(aColors[i].indexOf(oInputField.value) == 0)
- aResult.push(aColors[i]); //加入結果
- }
- if(aResult.length > 0) //若是有匹配的顏色則顯示出來
- setColors(aResult);
- else //不然就清除、用戶多輸入一個字母
- clearColors(); //就有可能從有匹配到無、到無的時候須要清除
- }
- else
- clearColors(); //無輸入時清除提示框
- }
- function clearColors(){
- //清除提示內容
- for(var i = oColorsUl.childNodes.length - 1 ; i >= 0 ; i-- )
- oColorsUl.removeChild(oColorsUl.childNodes[i]);
- oPopDiv.className = "hide" ;
- }
- function setColors(the_colors){
- //顯示提示框、傳入的參數即爲匹配出來的結果組成的數組
- clearColors(); //沒輸入一個字母就先清楚原先的提示、再繼續
- oPopDiv.className = "show" ;
- var oLi ;
- for(var i = 0 ; i < the_colors.length ; i++ ){
- //將匹配的提示結果逐一顯示給用戶
- oLi = document.createElement("li");
- oColorsUl.appendChild(oLi);
- oLi.appendChild(document.createTextNode(the_colors[i]));
- oLi.onmouseover = function(){
- this.className = "mouseOver" ; //鼠標指針通過時高亮
- }
- oLi.onmouseout = function(){
- this.className = "mouseOut" ; //鼠標指針離開時恢復原樣
- }
- oLi.onclick = function(){
- //用戶單擊某個匹配項時、設置輸入框爲該項的值
- oInputField.value = this.firstChild.nodeValue;
- clearColors(); //同時清除提示框
- }
- }
- }
- </script>
- </head>
- <body>
- <form method="post" name="myForm1">
- Color:<input type="text" name="colors" id="colors" onkeyup="findColors();" />
- </form>
- <div id="popup">
- <ul id="colors_ul"></ul>
- </div>
- </body>
- </html>