盗用百度词库实现智能匹配

<textarea class="xhtml:nogutter" name="code" rows="15" cols="50">&lt;html&gt;&lt;head&gt;&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;charset=gb2312&quot;&gt;&lt;mce:style&gt;&lt;!--#wd { border:solid 1px #999; font-size:14px; padding:4px; }#wd_box { border:solid 1px #999; background-color:#fff; position:absolute; z-index:1001px;}#wd_box a { display:block; padding:4px; color:#000; cursor:default; text-decoration:none; font-size:13px; }#wd_box a:hover { background-color:#0066cc; color:#fff; }--&gt;&lt;/mce:style&gt;&lt;style mce_bogus=&quot;1&quot;&gt;#wd { border:solid 1px #999; font-size:14px; padding:4px; }#wd_box { border:solid 1px #999; background-color:#fff; position:absolute; z-index:1001px;}#wd_box a { display:block; padding:4px; color:#000; cursor:default; text-decoration:none; font-size:13px; }#wd_box a:hover { background-color:#0066cc; color:#fff; }&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;input id=&quot;wd&quot; type=&quot;text&quot; size=&quot;50&quot; value=&quot;&quot; /&gt;&lt;input type=&quot;button&quot; value=&quot;百度搜索&quot; οnclick=&quot;search()&quot; /&gt;&lt;mce:script type=&quot;text/javascript&quot;&gt;&lt;!--var K = document.getElementById(&quot;wd&quot;);//获取匹配项function getword(){hideD();var V = K.value;if(!V) return;var doma = [];doma.push(&quot;sugge.stion&quot;);doma.push(&quot;baidu&quot;);doma.push(&quot;com&quot;);var U = 3;var C = document.createElement(&quot;SCRIPT&quot;);C.src=&quot;http://&quot;+doma.join('.').replace('.','')+&quot;/su?wd=&quot;+encodeURIComponent(V)+&quot;&amp;p=&quot;+U+&quot;&amp;t=&quot;+(new Date()).getTime();C.charset=&quot;gb2312&quot;;document.body.appendChild(C);}if(window.baidu==null) window.baidu={};baidu.sug = function(data){ len = data.s.length; if(len==0)return; var s = []; var vd = &quot;javascript:void(0);&quot;; var focus = &quot;javascript:setV(this);&quot;; var blur = &quot;javascript:leaveA(this);&quot;; for(var i=0;i&lt;data.s.length;i++){ s.push(&quot;&lt;a lang='&quot;+i+&quot;' href=&quot;&quot;+vd+&quot;&quot; mce_href=&quot;&amp;quot;+vd+&amp;quot;&quot; οnfοcus='&quot;+focus+&quot;' οnblur='&quot;+blur+&quot;' οnclick='&quot;+focus+&quot;hideD();'&gt;&quot;+data.s[i]+&quot;&lt;/a&gt;&quot;); } show(s);};//显示匹配项function show(s){var div = document.createElement(&quot;div&quot;);div.style.left = K.offsetLeft+&quot;px&quot;;div.style.top = K.offsetTop+20+&quot;px&quot;;div.style.width = K.offsetWidth+&quot;px&quot;;div.id = &quot;wd_box&quot;;div.innerHTML = s.join('');document.body.appendChild(div);div.onmouseover = function(){overD=true;};div.onmouseout = function(){overD=false;};overD = true;}//为搜索框赋值function setV(e){ K.value = e.innerHTML; indx = parseInt(e.lang); e.style[&quot;backgroundColor&quot;] = &quot;#0066cc&quot;; e.style[&quot;color&quot;] = &quot;#ffffff&quot;;}function leaveA(e){ e.style[&quot;backgroundColor&quot;] = &quot;#ffffff&quot;; e.style[&quot;color&quot;] = &quot;#000000&quot;;}//隐藏匹配层var overD = false;function hideD(){ var div = document.getElementById(&quot;wd_box&quot;); if(div) document.body.removeChild(div); indx = -1; len = 0;}//搜索function search(){ window.location = &quot;http://www.baidu.com/s?wd=&quot;+K.value;}//关键字改变时自动匹配var kv = &quot;&quot;;setInterval(function(){ if(indx==-1 &amp;&amp; K.value!=kv){ kv = K.value; getword(); }},100);//初始化控件事件window.onload = function(){document.body.onclick = function(){ if(!overD) hideD();};K.onkeydown = function(){indx=-1;};K.onmouseover = function(){overD=true;};K.onmouseout = function(){overD=false;};}//设置匹配项焦点function setFocus(){ var arr = document.getElementById(&quot;wd_box&quot;).getElementsByTagName(&quot;A&quot;); arr[indx].focus();}var indx = -1;//焦点所在索引var len = 0;//匹配项个数//按下上下键document.οnkeydοwn=function(ev){ev = ev || event;var code = (ev.keyCode||ev.which);if(code==9||code==13){ search(); return;}if(code==38){//up if(indx&lt;=0)return; indx--; setFocus();}else if(code==40){//down if(indx+1==len)return; indx++; setFocus();}};// --&gt;&lt;/mce:script&gt;&lt;/body&gt;&lt;/html&gt;</textarea> 


作者:朱会震


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值