


JavaScript imitation Baidu input box prompts automatic drop-down completion_javascript skills
The example in this article explains the automatic pull-down completion operation of the JavaScript input box, imitating the Baidu and Google search box prompts. The specific content is as follows
Rendering:
Specific code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> /*自动下拉补全 zhk */ var highlightindex=-1;//当前高亮的节点 $(document).ready(function(){ var wordInput=$("#word"); var wordInputOffset=wordInput.offset(); $("#auto").hide().css("border","1px black solid").css("position","absolute") .css("top",wordInputOffset.top+wordInput.height()+5+"px") .css("left",wordInputOffset.left+"px").width(wordInput.width()+2); wordInput.keyup(function (event){ var myEvent=event||window.event; var keyCode=myEvent.keyCode; if(keyCode>=65&&keyCode<=90||keyCode==8||keyCode==46){ var wordText=$("#word").val(); var autoNode=$("#auto"); if(wordText!=""){ var wordNodes=$("span"); autoNode.html(""); wordNodes.each(function(i){ var wordNode=$(this); var newDivNode=$("<div>").attr("id",i); newDivNode.html(wordNode.text()).appendTo(autoNode); newDivNode.mouseover(function(){//鼠标进入 if(highlightindex!=-1){ $("#auto").children("div").eq(highlightindex) .css("background-color","white"); } highlightindex=$(this).attr("id"); $(this).css("background-color","red"); }) newDivNode.mouseout(function(){//鼠标移除 $(this).css("background-color","white"); }) newDivNode.click(function(){//点击 var comText=$(this).text(); $("#auto").hide(); highlightindex=-1; $("#word").val(comText); }) }) if(wordNodes.length>0){ autoNode.show(); }else{ autoNode.hide(); highlightindex=-1; } }else{ autoNode.hide(); highlightindex=-1; } }else if(keyCode==38||keyCode==40){ if(keyCode==38){//向上 var autoNodes=$("#auto").children("div"); if(highlightindex!=-1){ autoNodes.eq(highlightindex).css("background-color","white"); highlightindex--; }else{ highlightindex=autoNodes.length-1; } if(highlightindex==-1){ highlightindex=autoNodes.length-1; } autoNodes.eq(highlightindex).css("background-color","red"); } if(keyCode==40){ var autoNodes=$("#auto").children("div"); if(highlightindex!=-1){ autoNodes.eq(highlightindex).css("background-color","white"); } highlightindex++; if(highlightindex==autoNodes.length){ highlightindex=0; } autoNodes.eq(highlightindex).css("background-color","red"); } }else if(keyCode==13){ if(highlightindex!=-1){ var comText=$("#auto").hide().children("div").eq(highlightindex).text(); highlightindex=-1; $("#word").val(comText); }else{ alert("文本框中的【"+$("#word").val()+"】被提交了"); $("#auto").hide(); $("#word").get(0).blur();//失去焦点 } } }); $("input [type='button']").click(function(){ alert("文本框中的【"+$("#word").val()+"】被提交了"); }); }) </script> </HEAD> <BODY> <input type="text" id="word"> <input type="button" value="提交"> <div id="auto"></div> <p> <span>aaa</span> <span>abc</span> <span>abd</span> <span>bbc</span> <span>beb</span> <span>cer</span> <span>erd</span> <span>beg</span> <p> </BODY> </HTML>
The above is the entire content of this article. I hope it will be helpful to everyone in learning javascript programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What should I do if Google Chrome prompts that the content of this tab is being shared? When we use Google Chrome to open a new tab, we sometimes encounter a prompt that the content of this tab is being shared. So what is going on? Let this site provide users with a detailed introduction to the problem of Google Chrome prompting that the content of this tab is being shared. Google Chrome prompts that the content of this tab is being shared. Solution: 1. Open Google Chrome. You can see three dots in the upper right corner of the browser "Customize and control Google Chrome". Click the icon with the mouse to change the icon. 2. After clicking, the menu window of Google Chrome will pop up below, and the mouse will move to "More Tools"

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Baidu Tieba app prompts that the operation is too frequent. This prompt is usually to maintain the normal operation and user experience of the platform to prevent malicious screen spam, advertising spam and other inappropriate behaviors. For specific handling methods, you can read the tutorial shared by the editor. Baidu Tieba app prompts that the operation is too frequent. Sharing how to deal with it 1. When the system prompts [Operation is too frequent], we need to wait for a while. If you are anxious, you can do something else first. Generally, after waiting for a while, this prompt message will It will disappear automatically and we can use it normally. 2. If after waiting for a long time, it still displays [Operation Too Frequent], we can try to go to Tieba Emergency Bar, Tieba Feedback Bar and other official Tieba, post to report this phenomenon and ask official personnel to solve it. 3.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
