欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery实现模拟搜索引擎的智能提示功能简单示例

程序员文章站 2023-12-06 08:26:52
本文实例讲述了jquery实现模拟搜索引擎的智能提示功能。分享给大家供大家参考,具体如下: jquery中模仿搜索引擎的智能提示功能,本案例仅供初学者一个参考,也是我个人...

本文实例讲述了jquery实现模拟搜索引擎的智能提示功能。分享给大家供大家参考,具体如下:

jquery中模仿搜索引擎的智能提示功能,本案例仅供初学者一个参考,也是我个人在初学jquery时写的一个初学案例。有不当之处,敬请指教。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>www.jb51.net jquery模拟搜索提示</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
    var arr=['小李','小二','小三','老林','老王','二哈','小强'];
      var focus=false;
      $(document).ready(function(){
        $("#txt").bind("focus",function(){
          $("#auto").show();
          abc();
        });
        $("#txt").bind("keyup",function(){
          abc();
        });
        $("#txt").bind("blur",function(){
          $("#auto").hide();
        });
        function abc(){
          var auto=$("#auto");
          var txt=$("#txt").val();
          var new_arr=new array();
          var n=0;
          for(i in arr){
            if(arr[i].indexof($("#txt").val())>=0){
              new_arr[n++]=arr[i];
            }
          }
          auto.empty();
          for(i in new_arr){
            var divnode=$("<div>").attr("id",i);
            divnode.attr("style","width:100px;height:30px;border: 1px solid red;");
            divnode.mouseover(function(){
              $(this).css("background-color","white");
            });
            divnode.mouseleave(function(){
              $(this).css("background-color","white");
            });
            divnode.click(function(){
              $("#txt").val($(this).html());
            });
            divnode.html(new_arr[i]);
            auto.append(divnode);
          }
        }
      });
    </script>
  </head>
  <body>
    <div>
    <input type="text" id="txt"/>
    <div id="auto"></div>
    </div>
  </body>
</html>

运行效果:

jQuery实现模拟搜索引擎的智能提示功能简单示例

感兴趣的朋友可以使用在线html/css/javascript代码运行工具http://tools.jb51.net/code/htmljsrun测试上述代码运行效果。

更多关于jquery相关内容感兴趣的读者可查看本站专题:《jquery页面元素操作技巧汇总》、《jquery常见事件用法与技巧总结》、《jquery常用插件及用法总结》、《jquery扩展技巧总结》及《jquery选择器用法总结

希望本文所述对大家jquery程序设计有所帮助。