小巧适用的AJAX英汉汉英字典

俺们程序员在工作中常常会查查字典,找一下英文单词怎么拼的或者看看单词的意思。这个时候我们必然要使用一些字典软件,如金山词霸之类,或者去一些查单词的网站,到达查询页面后,输入单词,等待刷新之后才能获得查询的结果。

这里送给大家一款AJAX的英汉汉英字典,体积小、纯HTML打造、无需安装、反应速度快、方便快捷、功能强大,是居家旅行、杀人放火之必备良药。那么,请问这么好的字典,哪里有买的呢?且看:

None.gif < html >
None.gif
< meta  http-equiv =Content-Type  content ="text/html;charset=gb2312" >
None.gif
< head >
None.gif    
< link  href ="http://www.dict.cn/img/search.css"  rel ="stylesheet"  type ="text/css" >
None.gif    
< script  src ='http://www.dict.cn/img/search.js'  language ='javascript' ></ script >
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
< script  language ="javascript" > dot.gif
InBlock.gif 
InBlock.gif     window.onload 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif     
var xmlHttp = false;
InBlock.gif     
var e;
InBlock.gif     
InBlock.gif     
//创建XMLHTTP对象
InBlock.gif
     function getXMLHTTPObj()
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif        
var C = null;
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            C 
= new ActiveXObject("Msxml2.XMLHTTP");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch(e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                C 
= new ActiveXObject("Microsoft.XMLHTTP");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(sc)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                C 
= null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
if!&& typeof XMLHttpRequest != "undefined" )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            C 
= new XMLHttpRequest();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
return C;
ExpandedSubBlockEnd.gif     }

InBlock.gif     
InBlock.gif     
//调用远程方法
InBlock.gif
     function callServer(e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif
InBlock.gif        document.getElementById(
"lookup_result").style.display = "none";
InBlock.gif        
//获取查询日期
InBlock.gif
        var lookup_obj = document.getElementById("word");
InBlock.gif        
var lookup_word = lookup_obj.value;
InBlock.gif        
InBlock.gif        LookUp(lookup_word);
ExpandedSubBlockEnd.gif     }

InBlock.gif     
InBlock.gif     
//查字典
InBlock.gif
     function LookUp(sIn) 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif
InBlock.gif        
var lookup_obj = document.getElementById("word");
InBlock.gif
InBlock.gif        lookup_obj.value 
= sIn.split("&")[0];
InBlock.gif        
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( xmlHttp && ( xmlHttp.readyState != 0 ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                xmlHttp.abort();
ExpandedSubBlockEnd.gif            }

InBlock.gif    
InBlock.gif            xmlHttp 
= getXMLHTTPObj();
InBlock.gif    
InBlock.gif            
if( xmlHttp )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//构造查询连接字符串
InBlock.gif
                var url = "http://www.dict.cn/search/?q=" + sIn;
InBlock.gif                
//打开连接
InBlock.gif
                xmlHttp.open("GET", url, true);
InBlock.gif                
//设置回调函数
InBlock.gif
                xmlHttp.onreadystatechange = updatePage;
InBlock.gif                
//发送请求
InBlock.gif
                xmlHttp.send(null);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                document.getElementById(
"flag").innerHTML = "XMLHTTP对象创建失败";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            document.getElementById(
"flag").innerHTML = "查询错误:" + e;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif     }

InBlock.gif     
InBlock.gif     
//回调处理函数
InBlock.gif
     function updatePage() 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (xmlHttp.readyState == 1
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
//        document.getElementById("flag").innerHTML = "正在加载连接对象dot.gifdot.gif";
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
InBlock.gif            
if (xmlHttp.readyState == 2
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
//        document.getElementById("flag").innerHTML = "连接对象加载完毕。";
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
InBlock.gif            
if (xmlHttp.readyState == 3
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
//        document.getElementById("flag").innerHTML = "数据获取中dot.gifdot.gif";
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
InBlock.gif            
if (xmlHttp.readyState == 4
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
//            document.getElementById("flag").innerHTML = "数据获取成功";
InBlock.gif
            
InBlock.gif                
var response = bytes2BSTR(xmlHttp.responseBody) ;
InBlock.gif                
var OpenValue = response.split("<big><b>")[1];
InBlock.gif                
var OpenValue = OpenValue.split("<td align=\"right\">")[0];
InBlock.gif                
var OpenValue = ReplaceAll(OpenValue,"/img/","http://www.dict.cn/img/");
InBlock.gif                
InBlock.gif                
//替换 近似解释 的查询链
InBlock.gif
                var OpenValue = ReplaceAll(OpenValue,"href=./?q=","href=javascript:LookUp(\"");
InBlock.gif                var OpenValue = ReplaceAll(OpenValue,
">&lt;<font color=#223322","\")>&lt;<font color=#223322");
InBlock.gif                
InBlock.gif                
//替换 同义词 的查询链接
InBlock.gif
                var restr = /<a href="index.php\?q=(.*?)">(.*?)<\/a>/g;
InBlock.gif                
var newstr = "<a href=\"javascript:LookUp('$1')\">$2</a>";
InBlock.gif                
var OpenValue = OpenValue.replace(restr,newstr);
InBlock.gif                
InBlock.gif                
//屏蔽 添加到单词本
InBlock.gif
                var OpenValue = ReplaceAll(OpenValue,"src=http://www.dict.cn/img/wordbook.gif","src=/img/wordbook.gif width=0");
InBlock.gif                
var OpenValue = ReplaceAll(OpenValue,"加入生词本","");
InBlock.gif                
InBlock.gif                document.getElementById(
"flag").innerHTML = OpenValue;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          document.getElementById(
"flag").innerHTML = "回调处理错误:" + e;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif     }

InBlock.gif 
InBlock.gif    
function enterToTab()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(event.keyCode == 13)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            event.keyCode 
= 9;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    
</ script >
ExpandedBlockStart.gifContractedBlock.gif    
< script  type ="text/vbscript"  language ="vbscript" > dot.gif
InBlock.gif    Function ReplaceAll(sIn,sFind,sReplace)
InBlock.gif        strReturn 
= ""
InBlock.gif        strReturn 
= Replace(sIn,sFind,sReplace)
InBlock.gif        ReplaceAll 
= strReturn
InBlock.gif    End Function 
InBlock.gif
InBlock.gif    Function bytes2BSTR(vIn)
InBlock.gif        strReturn 
= ""
InBlock.gif        For i 
= 1 To LenB(vIn)
InBlock.gif            ThisCharCode 
= AscB(MidB(vIn,i,1))
InBlock.gif            If ThisCharCode 
< &H80 Then
InBlock.gif                strReturn 
= strReturn & Chr(ThisCharCode)
InBlock.gif            Else
InBlock.gif                NextCharCode 
= AscB(MidB(vIn,i+1,1))
InBlock.gif                strReturn 
= strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
InBlock.gif                i 
= i + 1
InBlock.gif            End If
InBlock.gif        Next
InBlock.gif        bytes2BSTR 
= strReturn
InBlock.gif    End Function 
ExpandedBlockEnd.gif    
</ script >
None.gif
</ head >
None.gif
< body >
None.gif    
< form >
None.gif        
< span  class ="f pl30" > 请输入要查询的汉字或者英文: </ span >< br />
None.gif        
< input  type ="text"  id ="word"  onblur ="callServer(this);"  onkeydown ="enterToTab();"  onmouseover ="this.focus()"  onfocus ="this.select()"  value ="english"  accesskey ="c"   />
None.gif        
< div  id ="lookup_result" >
None.gif        
</ div >
None.gif        
< div  id ="flag" >
None.gif        
</ div >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >
None.gif

拷贝以上代码,另存为一个html的文件,字典便有了。
界面相当的简单,只有一个输入框,填入你想要查询的单词,Tab或者回车一下,单词解释会立马显示出来。汉语的词有相近单词;英语的话有近似词,均可直接点击查询哦。

最后再次感谢一下我们的字典数据来源: www.dict.cn ,网站的确做的不错,是个学英语的好去处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值