<Li> on the page, when you enter any letter, press the button next to it to find the words that contain your input. <li> to test the program performance, you can change the elements in the array (the search is performed on the array, so the words in <li> are irrelevant to the search .)
<Html>
<Head>
<Title> Javascript fuzzy search </title>
</Head>
<Body>
<Li onload = "load ('name')" id = "Name"> name </li>
<Li onload = "load ('sex ')" id = "sex"> sex </li>
<Li onload = "load ('age')" id = "age"> age </li>
<Li onload = "load ('job')" id = "job"> job </li>
<Li onload = "load ('mail')" id = "mail"> E-mail </li>
<Input id = "input" type = "text" value = ""/>
<Input id = "search" type = "button" onclick = "findEach ()" value = "Search"/>
<Script>
Var vData = ["name", "sex", "age", "job", "E-mail"];
Function load (id)
{
Alert (vData [0]);
// VData [vData. length] = document. getElementById (id). innerHTML;
}
Function find (sFind, sObj)
{
Var nSize = sFind. length;
Var nLen = sObj. length;
Var sCompare;
If (nSize <= nLen ){
For (var I = 0; I <= nLen-nSize; I ++ ){
SCompare = sObj. substring (I, I + nSize );
If (sCompare = sFind ){
Return I;
}
}
}
Return-1;
}
Function findEach ()
{
Var sFind = document. getElementById ("input"). value;
If (sFind = ""){
Alert ("Can not be empty ");
}
If (sFind! = ""){
Var nPos;
Var vResult = [];
// For (var I = 0; I <= vData. length; I ++ ){
For (var I in vData ){
// NPos = find (sFind, vData [I]);
Var sTxt = vData [I] | '';
NPos = sTxt. indexOf (sFind );
If (nPos> = 0 ){
VResult [vResult. length] = sTxt;
}
}
Alert (vResult );
}
}
</Script>
</Body>
</Html>
Author: yorhomwang