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