教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 js实现输入框自动匹配字符的实例代码

js实现输入框自动匹配字符的实例代码

发布时间:2014-12-19   编辑:jiaochengji.com
本文介绍下,js实现输入框自动匹配字符的一个例子,类似论坛中的自动匹配输入框。有需要的朋友参考下。

1,输入框内容

<p><span style="font-size: 18px; ">输入框自动匹配字符~</span></p><p><span style="font-size: 18px; ">
问题:所有匹配的字符要事先写好~大型网站的话,请考虑用数据库记录苏或其它高级方法。</span></p>
<p><span style="font-size: 18px; ">看代码</span></p>

2,匹配代码部分

<style>
ul,li{margin: 0; padding: 0;width:100%;}
</style>
<script language="javascript" type="text/javascript">
/**
* 输入框自动匹配字符
* edit www.jbxue.com
*/
function checkword()
{
var wordvalue=document.getElementById("word").value.toLowerCase();
var alltxt="all|big|cut|car\|daph8|eeg|egg|eat|fuck|fuck you|fix|good|
hand|hidden|ill|jack|jad|kevin|long|man|number|oio
|part|pp|quit|QQ|rest|reg|set|submit|time|tag|uuzo
|view|windows|want|xy|xun|young|yuyu|z|
David|David|哈哈|";//要匹配的字符,仅用于小型论坛
var alltxtalltxtpp=alltxt.toLowerCase();
var alltxtalltxt_xiang=alltxt.split("|");
var alltxt_xiang1=alltxtpp.split("|");
var inhtml="<ul>"
var isyou=0;
for (i=0;i<alltxt_xiang1.length;i++)
{
if (alltxt_xiang1[i].substr(0,wordvalue.length)==wordvalue)
{
inhtmlinhtml=inhtml+"<li onclick=\"document.getElementById('word').value=this.innerHTML;document.getElementById('showmenu').style.display='none';\" 
onmouseover=\"this.style.backgroundColor='#666666'\" onmouseout=\"this.style.backgroundColor=''\">"+alltxt_xiang[i]+"</li>";
isyou=1;
}
}
inhtmlinhtml=inhtml+"</ul>";
if (isyou==1)
{
document.getElementById("showmenu").innerHTML=inhtml;
document.getElementById("showmenu").style.display="";
}
else
{
document.getElementById("showmenu").innerHTML="";
document.getElementById("showmenu").style.display="none";
}
if (wordvalue=="")
{
document.getElementById("showmenu").innerHTML="";
document.getElementById("showmenu").style.display="none";
}
}
</script>
<input type="text" name="word" size="20" id="word" onkeyup="checkword()" style="border:1px solid #666666;width:200px;height:25px;">
<div style="position: absolute; width: 200px; height: 100px; z-index: 1; left: 10px; top: 40px;border:1px solid #666666;display:none;"
 id="showmenu"></div> 

您可能感兴趣的文章:
js实现输入框自动匹配字符的实例代码
LZ77算法的JS加密实现
js去掉字符串的空格或换行符(附相关正则介绍)
.net开发中常用正则表达式
js控制文本框自动适应文本长度的例子
正则表达式在网络编程中的运用
常用js正则表达式验证代码
js 自动提示的文本框特效代码
正则表达式(匹配中文、数字、html等)代码大全
jQuery-ui中自动完成实现方法

[关闭]
~ ~