?今天在公司做一個電子商務網(wǎng)站的注冊會員時,要求用戶在電子郵箱文本框中輸入時,給與熱點提示常用的電子郵箱,幫助用戶選擇,提高體驗效果。下面是用Jquery+css實現(xiàn)的郵箱自動補全,供大家參考和學習。
HTML代碼:emailAutoComple.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>郵箱自動補全</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href="css/emailAutoComple.css"> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="js/emailAutoComple.js"></script> <script type="text/javascript"> $(function(){ $.AutoComplete("#email"); //(要補全文本框的id) }); </script> </head> <body> <form action=""> 姓名:<input type="text" name="userName" id="userName"/><br/> 郵箱:<input type="text" name="email" id="email"/> </form> </body> </html>
JS代碼:js/emailAutoComple.js(實現(xiàn)自動補全的關鍵代碼)
jQuery.AutoComplete = function(selector){ var elt = $(selector); var strHtml = '<div class="AutoComplete" id="AutoComplete">'+ ' <ul class="AutoComplete_ul">'+ ' <li hz="@163.com"></li>'+ ' <li hz="@126.com"></li>'+ ' <li hz="@139.com"></li>'+ ' <li hz="@189.com"></li>'+ ' <li hz="@qq.com"></li>'+ ' <li hz="@vip.sina.com"></li>'+ ' <li hz="@sina.cn"></li>'+ ' <li hz="@sina.com"></li>'+ ' <li hz="@sohu.com"></li>'+ ' <li hz="@hotmail.com"></li>'+ ' <li hz="@gmail.com"></li>'+ ' <li hz="@wo.com.cn"></li>'+ ' <li hz="@21cn.com"></li>'+ ' <li hz="@aliyun.com"></li>'+ ' <li hz="@yahoo.com"></li>'+ ' <li hz="@foxmail.com"></li>'+ ' </ul>'+ ' </div>'; //將div追加到body上 $('body').append(strHtml); var autoComplete,autoLi; autoComplete = $('#AutoComplete'); autoComplete.data('elt',elt); autoLi = autoComplete.find('li'); autoLi.mouseover(function(){ $(this).siblings().filter('.hover').removeClass('hover'); $(this).addClass('hover'); }).mouseout(function(){ $(this).removeClass('hover'); }).mousedown(function(){ autoComplete.data('elt').val($(this).text()).change(); autoComplete.hide(); }); //用戶名補全+翻動 elt.keyup(function(e){ if(/13|38|40|116/.test(e.keyCode) || this.value==''){ return false; } var username = this.value; if(username.indexOf('@')==-1){ autoComplete.hide(); return false; } autoLi.each(function(){ this.innerHTML = username.replace(/\@+.*/,'')+$(this).attr('hz'); if(this.innerHTML.indexOf(username)>=0){ $(this).show(); }else{ $(this).hide(); } }).filter('.hover').removeClass('hover'); autoComplete.show().css({ left : $(this).offset().left, top : $(this).offset().top + $(this).outerHeight(true) - 1, position: 'absolute', zIndex: '99999' }); if(autoLi.filter(':visible').length==0){ autoComplete.hide(); }else{ autoLi.filter(':visible').eq(0).addClass('hover'); } }).keydown(function(e){ if(e.keyCode==38){ //上 autoLi.filter('.hover').prev().not('.AutoComplete_title').addClass('hover').next().removeClass('hover'); }else if(e.keyCode==40){ //下 autoLi.filter('.hover').next().addClass('hover').prev().removeClass('hover'); }else if(e.keyCode==13){ //確定 autoLi.filter('.hover').mousedown(); } }).focus(function(){ autoComplete.data('elt',$(this)); }).blur(function(){ autoComplete.hide(); }); };
CSS代碼:css/emailAutoComple.css
#AutoComplete{background:#fff;border:1px solid #4190db;display:none;width:150px;} #AutoComplete ul{list-style-type:none;margin:0;padding:0;} #AutoComplete li{color:#333;cursor:pointer;font:12px/22px \5b8b\4f53;text-indent:5px;} #AutoComplete .hover{background:#6eb6fe;color:#fff;}
? ?
效果圖:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

Hot Article
Guide: Stellar Blade Save File Location/Save File Lost/Not Saving
4 weeks ago
By DDD
Oguri Cap Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Agnes Tachyon Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Dune: Awakening - Advanced Planetologist Quest Walkthrough
4 weeks ago
By Jack chen
Date Everything: Dirk And Harper Relationship Guide
4 weeks ago
By Jack chen

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)
