利用js 進(jìn)行輸入框自動(dòng)匹配字符的小例子
更新時(shí)間:2013年06月29日 16:01:20 作者:
制作論壇用到的輸入框~想模仿百度之類的寫(xiě)一個(gè)自動(dòng)匹配字符
html
<p><span style="font-size: 18px; ">輸入框自動(dòng)匹配字符~</span></p><p><span style="font-size: 18px; ">但是遇到一個(gè)問(wèn)題~所有匹配的字符要事先寫(xiě)好~大概如果是大型網(wǎng)站的話~應(yīng)該是利用數(shù)據(jù)庫(kù)的記錄吧</span></p><p><span style="font-size: 18px; ">看代碼</span></p>
html
<style>
ul,li{margin: 0; padding: 0;width:100%;}
</style>
<script language="javascript" type="text/javascript">
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>
復(fù)制代碼 代碼如下:
<p><span style="font-size: 18px; ">輸入框自動(dòng)匹配字符~</span></p><p><span style="font-size: 18px; ">但是遇到一個(gè)問(wèn)題~所有匹配的字符要事先寫(xiě)好~大概如果是大型網(wǎng)站的話~應(yīng)該是利用數(shù)據(jù)庫(kù)的記錄吧</span></p><p><span style="font-size: 18px; ">看代碼</span></p>
html
復(fù)制代碼 代碼如下:
<style>
ul,li{margin: 0; padding: 0;width:100%;}
</style>
<script language="javascript" type="text/javascript">
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>
相關(guān)文章
javascript dragable的Move對(duì)象
一個(gè)dragable的Move對(duì)象,大家可以運(yùn)行下,測(cè)試看下效果。2009-08-08基于javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
這篇文章主要介紹了基于javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能的相關(guān)資料,需要的朋友可以參考下2016-01-01JS實(shí)現(xiàn)數(shù)組扁平化的8種方式總結(jié)
數(shù)組扁平化指的是將一個(gè)多層嵌套的數(shù)組,處理成只有一層的數(shù)組,本文為大家整理了8個(gè)常用的JS實(shí)現(xiàn)數(shù)組扁平化的方法,希望對(duì)大家有所幫助2023-08-08淺談 Webpack 如何處理圖片(開(kāi)發(fā)、打包、優(yōu)化)
這篇文章主要介紹了淺談 Webpack 如何處理圖片(開(kāi)發(fā)、打包、優(yōu)化),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05express+mockjs實(shí)現(xiàn)模擬后臺(tái)數(shù)據(jù)發(fā)送功能
這篇文章主要介紹了express+mockjs實(shí)現(xiàn)模擬后臺(tái)數(shù)據(jù)發(fā)送功能,需要的朋友可以參考下2018-01-01JavaScript面向?qū)ο笾薪涌趯?shí)現(xiàn)方法詳解
這篇文章主要介紹了JavaScript面向?qū)ο笾薪涌趯?shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了javascript面向?qū)ο蠼涌诘母拍?、?shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-07-07JavaScript實(shí)現(xiàn)簡(jiǎn)單的四則運(yùn)算計(jì)算器完整實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的四則運(yùn)算計(jì)算器,結(jié)合完整實(shí)例形式分析了javascript基于表單相應(yīng)實(shí)現(xiàn)加減乘除數(shù)學(xué)運(yùn)算的操作技巧,需要的朋友可以參考下2017-04-04