-
Notifications
You must be signed in to change notification settings - Fork 26
/
原生js输入邮箱提示(类微博).html
173 lines (169 loc) · 5.32 KB
/
原生js输入邮箱提示(类微博).html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>js输入邮箱提示demo</title>
<style type="text/css">
*{
margin:0;
padding:0;
}
body{
background-color: #2b3e53;
overflow:hidden;
}
#input{
width:300px;
height:35px;
display:block;
margin:0 auto;
margin-top: 100px;
border:3px #ef4c6c solid;
}
ul{
margin:0 auto;
width:300px;
background-color: #fff;
border:3px #ef4c6c solid;
display:none;
}
li{
list-style: none;
height:22px;
padding-left: 15px;
}
.active{
background-color: grey;
}
</style>
</head>
<body>
<div class="suggest">
<input id="input" type="text" placeholder="请输入邮箱/用户名/昵称" autocomplete="off"/>
<ul id="emails">
<li>请选择或继续输入..</li>
<li class="active" email=" "> </li>
<li email="@sina.com" class="item">@sina.com</li>
<li email="@163.com" class="item">@163.com</li>
<li email="@126.com" class="item">@126.com</li>
<li email="@vip.com" class="item">@vip.com</li>
<li email="@qq.com" class="item">@qq.com</li>
<li email="@yahoo.com" class="item">@yahoo.com</li>
<li email="@gmail.com" class="item">@gmail.com</li>
<li email="@hotmail.com" class="item">@hotmail.com</li>
<li email="@188.com" class="item">@188.com</li>
</ul>
</div>
<script type="text/javascript">
window.onload = function(){
var su = new suggest();
su.init();
};
function suggest(){
this.sInput = document.getElementById('input');
this.sUl = document.getElementById('emails');
this.sLi = document.getElementsByTagName('li');
}
suggest.prototype = {
init : function(){
this.toChange();
this.toBlur();
this.sele();
},
/*input输入事件*/
toChange : function(){
var ie = !-[1,];
var that = this;
if(ie){
this.sInput.onpropertychange = function(){
that.toShow();
}
}else{
this.sInput.oninput = function(){
that.toShow();
}
}
},
/*显示提示*/
toShow : function(){
var value = this.sInput.value;
/*用于匹配相应邮箱格式的正则*/
var re = new RegExp('@'+value.substring(value.indexOf('@')+1)+'');
this.sUl.style.display = 'block';
if(re.test(value)){
this.sLi[1].innerHTML = value;
for(var i=2;i<this.sLi.length;i++){
var sEmail = this.sLi[i].getAttribute('email');
/*输入@后匹配email*/
if(re.test(sEmail)){
this.sLi[i].innerHTMl = value.substring(0,'@')+sEmail;
this.sLi[i].style.display = 'block';
}else{
this.sLi[i].style.display = 'none';
}
}
}else{
this.sUl.style.display = 'block';
this.sLi[1].innerHTML = value;
for(var i=2;i<this.sLi.length;i++){
this.sLi[i].innerHTML = value + this.sLi[i].getAttribute('email');
}
}
},
/*失去焦点*/
toBlur : function(){
var that = this;
this.sInput.onblur = function(){
that.sUl.style.display = 'none';
}
},
/*鼠标键盘相关选择事件*/
sele : function(){
var iNow = 1;
if(this.sInput.value ==''){
this.sLi[1].className='item';
}
/*鼠标相关*/
for(var i=0;i<this.sLi.length;i++){
var that = this;
this.sLi[i].onmouseover = function(ev){
this.className = 'active';
};
this.sLi[i].onmouseout = function(ev){
this.className = 'item';
};
this.sLi[i].onmousedown = function(ev){
that.sInput.value = ev.target.innerHTML;
}
}
/*键盘相关*/
var that = this;
this.sInput.onkeydown = function(ev){
var ev = ev || window.event;
if(ev.keyCode == 38){ //上
if(iNow == 1){
iNow=that.sLi.length;
}
iNow--;
for(var i=1;i<that.sLi.length;i++){
that.sLi[i].className = 'item';
}
that.sLi[iNow].className = 'active';
}else if(ev.keyCode == 40){ //下
iNow++;
if(iNow == that.sLi.length){
iNow=1;
}
for(var i=1;i<that.sLi.length;i++){
that.sLi[i].className = 'item';
}
that.sLi[iNow].className = 'active';
}else if(ev.keyCode == 13){
that.sInput.value = that.sLi[iNow].innerHTML;
}
}
}
}
</script>
</body>
</html>