首頁 > 後端開發 > php教程 > Js實現帶有自動提示的文字方塊效果

Js實現帶有自動提示的文字方塊效果

WBOY
發布: 2016-07-25 09:12:45
原創
1248 人瀏覽過

分享二個自動提示的文字方塊效果的js實作程式碼。

例一,直接編寫AJAX 實作。 客戶端:

  1. Ajax實作自動提示的文字方塊_bbs.it-home.org
  2. Color:
複製程式碼

伺服器端(9-10.aspx ):

  1. Response.CacheControl = "無快取";
  2. Response.AddHeader("Pragma","no-cache");
  3. string sInput = Request[ "sColor"].Trim();
  4. if(sInput.Length == 0)
  5. return;
  6. 字串sResult = "";
  7. string[] aColors = new string[]{" aliceblue","antiquewith","海藍寶石","天藍色","米色","濃湯","黑色","blanchedalmond","藍色","藍紫色","黃銅",
  8. "青銅色","棕色","burlywood","cadetblue","黃綠色","巧克力","銅","珊瑚色","cornfloewrblue","玉米絲", "青色",
  9. "深藍色","深青色","darkgoldenrod","暗灰色","深綠色","深卡其色","darkmagenta","darkolivegreen","darkorchid",
  10. "darkorenge ", “深紅色”、“深鮭魚色”、“深海綠”、“深板藍”、“深板灰色”、“深綠松石色”、“深紫羅蘭色”、“深粉色”、
  11. 「深天藍」、「暗灰色」、「道奇藍」、「長石」 ,"firebrick","floralwhite","forestgreen","紫紅色","gainsboro","gold","goldenrod",
  12. "golenrod","gostwhite","gray","green","greenyellow ","蜜露","熱粉紅","印度紅","inen","象牙色","卡其色","薰衣草色",
  13. "薰衣草腮紅","草坪綠","檸檬雪紡","淺藍色"," lightcoral","lightcyan","lightgodenrod","lightgodenrodyellow"," lightgray",
  14. "lightgreen","lightpink","lightsalmon","lightseagreen","lightskyblue","lightslateblue","lightslategray", "lightsteelblue","lightyellow",
  15. "lime"," limegreen","magenta","magenta","maroom","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurpul" ,
  16. 「中海綠」、「中石板」、「中春綠」、「中綠松石」、「中紫紅色」、「午夜藍」、「薄荷奶油色」、
  17. 「迷霧玫瑰色」、「鹿皮鞋」、「納瓦荷白」、 「海軍藍」 ,"海軍藍","oldlace","olivedrab","橙色","蘭花","orengered","palegodenrod",
  18. "淡綠色","淡綠松石色","淡紫紅色","木瓜鞭","桃泡芙"、"秘魯"、"粉紅色"、"梅紅色"、"粉藍色"、"紫色"、"石英"、"紅色"、
  19. "玫瑰棕色"、"皇家藍色"、"馬鞍棕色"、"鮭魚色"、"沙棕色」、“猩紅色”、“海綠色”、“貝殼色”、“赭色”、“銀色” 、「天藍色」、「石板灰色」、
  20. 「雪色」、「春綠」、「鋼藍」、「棕褐色」、 「薊」、「番茄」、「綠松石」、「紫羅蘭」、「紫羅蘭紅」、「小麥」、「白煙」、「黃」、「黃綠」};
  21. for(int i=0;iif( aColors[i].IndexOf(sInput) == 0)
  22. sResult += aColors[i] + ", ”;
  23. }
  24. if(sResult.Length>0) //如果有符合項目
  25. sResult = sResult.Substring(0,sResult.Length-1); // 去掉最後一個「,」號碼
  26. Response.Write(sResult);
  27. %>
複製程式碼

例二,使用jQuery 實作。 客戶端:

  1. jQuery實作自動提示的文字方塊
  2. "));
  3. oColorsUl.find("li").click(function(){
  4. oInputField.val($(this) .text());
  5. clearColors();
  6. }).hover(
  7. function(){$(this).addClass("mouseOver");},
  8. function(){$ (this).removeClass("mouseOver");}
  9. );
  10. }
  11. function findColors(){
  12. initVars(); //初始化變數
  13. if(oInputField.val() .length > 0){
  14. //取得非同步資料
  15. $.get("14-10.aspx",{sColor:oInputField.val()},
  16. function(data){
  17. var aResult = new Array();
  18. if(data.length > 0){
  19. aResult = data.split(",");
  20. setColors(aResult); //顯示伺服器結果
  21. } bbs.it-home.org
  22. else
  23. clearColors();
  24. });
  25. }
  26. else
  27. clearColors(); //無輸入時清除提示框(例如使用者按del鍵)
  28. }
  29. Color:
    複製程式碼

    伺服器端(14-10.aspx ):

    1. Response.CacheControl = "無快取";
    2. Response.AddHeader("Pragma","no-cache");
    3. string sInput = Request[ "sColor"].Trim();
    4. if(sInput.Length == 0)
    5. return;
    6. 字串sResult = "";
    7. string[] aColors = new string[]{" aliceblue","antiquewith","海藍寶石","天藍色","米色","濃湯","黑色","blanchedalmond","藍色","藍紫色","黃銅","青銅",
    8. "棕色","burlywood","cadetblue","黃綠色","巧克力","銅","珊瑚色","cornfloewrblue","玉米絲", "青色","深藍色","深青色",
    9. "darkgoldenrod","darkgray","深綠色","深卡其色","darkmagenta","darkolivegreen","darkorchid","darkorenge","darkred" ,"深鮭魚色",
    10. "深海綠","深板藍","深板灰色","深綠松石","深紫羅蘭","深粉紅","深天藍","暗灰色","道奇藍",
    11. "長石" ,"firebrick","floralwhite","forestgreen","紫紅色","gainsboro","gold","goldenrod","golenrod"," gostwhite","gray","green",
    12. "greenyellow ","蜜露","粉紅色","印度紅","艾寧","象牙色","卡其色","薰衣草色","薰衣草腮紅","草坪綠","檸檬雪紡",
    13. "淺藍色"," lightcoral","lightcyan","lightgodenrod","lightgodenrodyellow","lightgray"," lightgreen","lightpink","lightsalmon",
    14. "lightseagreen","lightskyblue","lightslateblue","lightslategray", "lightsteelblue","lightyellow","lime","limegreen","magenta",
    15. "洋紅色","maroom","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurpul" ,"mediumseagreen",
    16. "mediumslateblue","mediumspringgreen","mediumquoisegreen","mediumspringise","mediumspringise","mediumspringise","mediumspringise","mediumspringise","mediumspringise","mediumspringise","mediumspringise","mediumspringS"," ,"mediumvioletred","midnightblue","mintcream","mistyrose",
    17. "鹿皮鞋","navajowhite","海軍藍" ,"navyblue","oldlace","olivedrab","orange", "orchid","orengered","palegodenrod","palegreen",
    18. "paleturquoise","palevioletred","papayawhip","peachpuff "、"秘魯"、"粉紅色"、"梅紅色"、"粉藍色"、"紫色"、"石英"、"紅色"、"玫瑰棕色"、
    19. "寶藍色"、"馬鞍棕色"、"鮭魚色"、"沙棕色」、「猩紅色」、 「海綠色」、「貝殼色」、「赭色」、「銀色」、「天藍色」、「石板灰色」、「雪色」、
    20. 「春綠色」、「鋼藍色」、「棕褐色」、 「薊」、「番茄」、「綠松石」、「紫羅蘭」、「紫羅蘭」、「小麥」、「白煙」、「黃色」、「黃綠」};
    21. for(int i=0;iif(aColors[i].IndexOf(sInput) == 0)
    22. sResult += aColors[i] + ", 」;
    23. }
    24. if(sResult.Length>0) //如果有匹配項
    25. sResult = sResult.Substring(0,sResult.Length-1); // 去掉最後一個「,」號碼
    26. Response.Write (sResult);
    27. %>
    複製程式碼


    來源:php.cn
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板