


jQuery realizes the automatic completion effect of text box email input_jquery
I have seen the effect of email auto-complete on most websites, but the quality varies. Today I suddenly saw a blog online. I feel that this plug-in is very good, so I wanted to write it and share it with everyone!
The rendering is as follows:
The complete demo code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>邮箱地址自动完成功能</title> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <style> .demo p {width:1000px; margin-left:auto; margin-right:auto;} .input{padding:12px; width:300px; border:1px solid #c2c2c2; border-radius:4px; box-shadow:0 0 1px #fff, inset 0 0 2px rgba(0,0,0,.15); background-color:#F2F2F2; font-size:14px;} .emailist{border:1px solid #bdbdbd; border-radius: 4px; background-color:#fff; color:#666; font-size:14px; list-style-type:0; padding:0; margin:0; overflow:hidden;} .emailist li{padding:2px 11px; cursor:pointer;} .emailist .on, .emailist li:hover{background-color:#eee;} </style> </head> <body> <div class="demo"> <p><input type="email" class="input inputMailList" name="email" placeholder="请输入邮箱" required autofocus autocomplete="off"></p> </div> </body> </html> <script src="js/jquery-1.6.2.min.js"></script> <script src="js/jquery.mailAutoComplete-4.0.js"></script> <script type="text/javascript"> $(".inputMailList").mailAutoComplete();//使用方法 </script>
js implementation effect code (jquery.mailAutoComplete-4.0.js) is as follows:
(function($) { $.fn.mailAutoComplete = function(options) { var defaults = { className: "emailist", email: ["qq.com","gmail.com","126.com","163.com","hotmail.com","yahoo.com","yahoo.com.cn","live.com","sohu.com","sina.com"], //邮件数组 zIndex: 11 }; // 最终参数 var params = $.extend({}, defaults, options || {}); // 是否现代浏览器 var isModern = typeof window.screenX === "number", visibility = "visibility"; // 键值与关键字 var key = { "up": 38, "down": 40, "enter": 13, "esc": 27, "tab": 9 }; // 组装HTML的方法 var fnEmailList = function(input) { var htmlEmailList = '', arrValue = input.value.split("@"), arrEmailNew = []; $.each(params.email, function(index, email) { if (arrValue.length !== 2 || arrValue[1] === "" || email.indexOf(arrValue[1].toLowerCase()) === 0) { arrEmailNew.push(email); } }); $.each(arrEmailNew, function(index, email) { htmlEmailList = htmlEmailList + '<li'+ (input.indexSelected===index? ' class="on"':'') +'>'+ arrValue[0] + "@" + email +'</li>'; }); return htmlEmailList; }; // 显示还是隐藏 var fnEmailVisible = function(ul, isIndexChange) { var value = $.trim(this.value), htmlList = ''; if (value === "" || (htmlList = fnEmailList(this)) === "") { ul.css(visibility, "hidden"); } else { isIndexChange && (this.indexSelected = -1); ul.css(visibility, "visible").html(htmlList); } }; return $(this).each(function() { this.indexSelected = -1; // 列表容器创建 var element = this; var eleUl = $('<ul></ul>').css({ position: "absolute", marginTop: element.offsetHeight, minWidth: element.offsetWidth - 2, visibility: "hidden", zIndex: params.zIndex }).addClass(params.className).bind("click", function(e) { var target = e && e.target; if (target && target.tagName.toLowerCase() === "li") { $(element).val(target.innerHTML).trigger("input"); $(this).css(visibility, "hidden"); element.focus(); // add on 2013-11-20 } }); $(this).before(eleUl); // IE6的宽度 if (!window.XMLHttpRequest) { eleUl.width(element.offsetWidth - 2); } // 不同浏览器的不同事件 isModern? $(this).bind("input", function() { fnEmailVisible.call(this, eleUl, true); }): element.attachEvent("onpropertychange", function(e) { if (e.propertyName !== "value") return; fnEmailVisible.call(element, eleUl, true); }); $(document).bind({ "click": function(e) { var target = e && e.target, htmlList = ''; if (target == element && element.value && (htmlList = fnEmailList(element, params.email))) { eleUl.css(visibility, "visible").html(htmlList); } else if (target != eleUl.get(0) && target.parentNode != eleUl.get(0)) { eleUl.css(visibility, "hidden"); } }, "keydown": function(e) { var eleLi = eleUl.find("li"); if (eleUl.css(visibility) === "visible") { switch (e.keyCode) { case key.up: { element.indexSelected--; if (element.indexSelected < 0) { element.indexSelected = -1 + eleLi.length; } e.preventDefault && e.preventDefault(); break; } case key.down: { element.indexSelected++; if (element.indexSelected >= eleLi.length) { element.indexSelected = 0; } e.preventDefault && e.preventDefault(); break; } case key.enter: { e.preventDefault(); eleLi.get(element.indexSelected) && $(element).val(eleLi.eq(element.indexSelected).html()); eleUl.css("visibility", "hidden"); break; } case key.tab: case key.esc: { eleUl.css("visibility", "hidden"); break; } } if (element.indexSelected !== -1) { eleUl.html(fnEmailList(element)); } } } }); }); }; })(jQuery);
The above is the entire content of this article, I hope it will be helpful to everyone’s study.

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











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing
