jQuery 可输入多个名字自动补全框 jQuery 可输入多个名字自动补全框,因为我要做一个补全框,里面存放多个用户名,就是每输入一个时候都是补全像谷歌搜索那样的,然后点上后就存到这个框了,然后继续填又一个补全然后点击就放到框里了,以此类推,这个框里面可以有很多名字,然后每个名字后面有个小叉,可以点击去掉点的这个名字。请各位大侠帮我做一个类子,发给我,谢谢,我邮箱是:[email protected],小弟新学,请多给我点注释,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用firebug 分析google 的实现方式,明白原理就好实现了!一个简单例子,输入字符串后,回车<!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>js 文本框效果</title><style type="text/css">.inputs{width:600px;height:auto; border:1px solid #ddd;margin:100px auto;line-height:20px;}.divInputs{width:auto;height:auto;line-height:20px;min-height:20px; }.divHistory{height:20px;width:auto;line-height:20px;float:left;display:inline; margin:0px 3px; background-color:#dee; word-break:keep-all;/* 不换行 */ white-space:nowrap;/* 不换行 */}.clear{clear:both;} </style></head><body><div class="inputs" id='inputs'><div class="divInputs" id='divInputs'><input type='text' value="" style="float:left;border:0px;height:20px; " id='txtInput'/><div class='clear'></div></div></div><input type='button' value="查看设置的值" id="btn"/></body><script type="text/javascript">window.onload=function(){ var inputs=[]; var txtInput = document.getElementById("txtInput"); var divInputs = document.getElementById("divInputs"); document.getElementById("btn").onclick=function(){ alert(inputs); } ; document.getElementById("inputs").onclick=function(){ txtInput.focus(); } ; txtInput.onkeydown=function(e) { e=e||event; if(e.keyCode==13){ // 回车 var input = txtInput.value.trim(); if(input==""){ return; } if(inputs.indexOf(input)>-1){ alert("内容已存在"); return; } var div = document.createElement("div"); div.innerHTML ="<span>" +input+"</span>"; div.className="divHistory"; var span = document.createElement("span"); span.style.cssText ="top:0px;right:0px;position:relative;z-index:99999;font-size:12px; border:1px solid red;"; span.innerHTML="X"; div.appendChild(span); divInputs.insertBefore(div,txtInput); inputs.push(input); txtInput.value =""; span.onclick=function(){ this.parentNode.parentNode.removeChild(this.parentNode); inputs.remove(input); }; } };};String.prototype.trim=function(){ return this.replace(/(^\s*)|(\s*$)/g,'');};Array.prototype.indexOf=function(value){ if(this!=null && this.length>0){ for(var i=0;i<this.length;i++) { if(this[i]==value){ return i;} } } return -1;};Array.prototype.removeAt=function(index){this.splice(index,1);}Array.prototype.remove=function(obj){var index=this.indexOf(obj);if (index>=0){this.removeAt(index);}} </script></html> ExtJs 中 Ext.MessageBox.alert() 显示字符串换行问题 js中click事件的问题,高手求破。 求教关于window.open()在不同浏览器出现的不同问题 【急急急】document.form1.???.value,不知道怎麽填合适~~! 关于IE是否真正打印的获取 javascript 读取xml文档时,提示拒绝访问 判断复选框的问题! 如何可以使用户点击一次“登录”按钮后,不能再重复按该“登录”按钮 帮忙1234 js不执行 如何给标签中的title属性设置样式? jsp 中怎样弹出对话框提示
一个简单例子,输入字符串后,回车<!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>js 文本框效果</title>
<style type="text/css">
.inputs{width:600px;height:auto; border:1px solid #ddd;margin:100px auto;line-height:20px;}
.divInputs{width:auto;height:auto;line-height:20px;min-height:20px; }
.divHistory{height:20px;width:auto;line-height:20px;float:left;display:inline; margin:0px 3px; background-color:#dee;
word-break:keep-all;/* 不换行 */
white-space:nowrap;/* 不换行 */}
.clear{clear:both;} </style>
</head>
<body><div class="inputs" id='inputs'>
<div class="divInputs" id='divInputs'>
<input type='text' value="" style="float:left;border:0px;height:20px; " id='txtInput'/>
<div class='clear'></div>
</div>
</div>
<input type='button' value="查看设置的值" id="btn"/>
</body>
<script type="text/javascript">window.onload=function()
{
var inputs=[];
var txtInput = document.getElementById("txtInput");
var divInputs = document.getElementById("divInputs");
document.getElementById("btn").onclick=function(){
alert(inputs);
} ;
document.getElementById("inputs").onclick=function(){
txtInput.focus();
} ;
txtInput.onkeydown=function(e)
{
e=e||event;
if(e.keyCode==13){ // 回车
var input = txtInput.value.trim();
if(input==""){
return;
}
if(inputs.indexOf(input)>-1){
alert("内容已存在");
return;
}
var div = document.createElement("div");
div.innerHTML ="<span>" +input+"</span>";
div.className="divHistory";
var span = document.createElement("span");
span.style.cssText ="top:0px;right:0px;position:relative;z-index:99999;font-size:12px; border:1px solid red;";
span.innerHTML="X";
div.appendChild(span);
divInputs.insertBefore(div,txtInput);
inputs.push(input);
txtInput.value ="";
span.onclick=function(){
this.parentNode.parentNode.removeChild(this.parentNode);
inputs.remove(input);
};
}
};
};
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g,'');
};
Array.prototype.indexOf=function(value){
if(this!=null && this.length>0){
for(var i=0;i<this.length;i++)
{
if(this[i]==value){ return i;}
}
}
return -1;
};
Array.prototype.removeAt=function(index){
this.splice(index,1);
}
Array.prototype.remove=function(obj){
var index=this.indexOf(obj);
if (index>=0){
this.removeAt(index);
}
}
</script>
</html>