javascript實(shí)現(xiàn)的基于金山詞霸網(wǎng)絡(luò)翻譯的代碼
更新時間:2010年01月15日 14:43:52 作者:
下面的這段代碼是基于金山詞霸網(wǎng)絡(luò)翻譯提供的接口,遠(yuǎn)程調(diào)用文件,可以作為一個自定義的在線查詢工具。
上圖:

注意下面的代碼,最好保存為utf-8格式的,要不容易出現(xiàn)亂碼。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript 金山詞霸在線網(wǎng)絡(luò)翻譯 </title>
</head>
<body>
<script type="text/javascript">
function $(id) { return document.getElementById(id); }
function callScript(url, loaded, error, charset) {
var script = document.createElement("script");
if (typeof charset == "string") script.charset = charset;
script.onreadystatechange = function() {
switch (this.readyState) {
case "complete":
case "loaded":
if (typeof loaded == "function") loaded();
if (script.parentNode) script.parentNode.removeChild(script);
break;
}
}
script.onload = function() {
if (typeof loaded == "function") loaded();
if (script.parentNode) script.parentNode.removeChild(script);
}
script.onerror = function() {
if (typeof error == "function") error();
if (script.parentNode) script.parentNode.removeChild(script);
}
script.type = "text/javascript";
script.defer = "true";
script.src = url;
var parent = document.getElementsByTagName("HEAD")[0] || document.documentElement;
if (parent && parent.insertBefore) parent.insertBefore(script, parent.firstChild);
}
function button_translateClick() {
var word = encodeURIComponent($("text_word").value);
if (!word) {
alert('');
$("text_word").focus();
return;
}
callScript("http://server.dict-co.iciba.com/jsInterface.php?uiType=0&w=" + word + "&type=6");
callScript("http://server.dict-co.iciba.com/jsInterface.php?uiType=0&w=" + word + "&dict=Dict,Tf,Enen,");
callScript("http://server.dict-co.iciba.com/jsInterface.php?uiType=0&w=" + word + "&dict=Dict,Tf,Enen,");
callScript("http://server.dict-co.iciba.com/jsInterface.php?uiType=0&w=" + word + "&type=2");
}
function text_wordKeydown(e) {
if (!e) e = window.event;
switch (e.keyCode | e.which | e.charCode) {
case 13:
button_translateClick();
break;
}
}
function LoveCallback(context) {
$("div_context").innerHTML = context;
}
function dictCallBack(context) {
$("div_head").innerHTML = context;
}
function tfCallback(context) {
$("div_thesaurus").innerHTML = context;
}
function En2enCallback(context) {
$("div_en2en").innerHTML = context;
}
function djCallback(context) {
$("div_dj").innerHTML = context;
}
function searchDictByWord(e) {
var element = typeof event != "undefined" ? event.srcElement : e.target;
$("text_word").value = element.innerHTML;
button_translateClick();
}
</script>
<input id="text_word" type="text" value="hello" onkeydown="text_wordKeydown(event)" />
<input type="button" value="搜索" onclick="button_translateClick()"/>
<div id="div_head"></div>
<div id="div_context"></div>
<div id="div_thesaurus"></div>
<div id="div_en2en"></div>
<div id="div_dj"></div>
</body>
</html>

注意下面的代碼,最好保存為utf-8格式的,要不容易出現(xiàn)亂碼。
復(fù)制代碼 代碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript 金山詞霸在線網(wǎng)絡(luò)翻譯 </title>
</head>
<body>
<script type="text/javascript">
function $(id) { return document.getElementById(id); }
function callScript(url, loaded, error, charset) {
var script = document.createElement("script");
if (typeof charset == "string") script.charset = charset;
script.onreadystatechange = function() {
switch (this.readyState) {
case "complete":
case "loaded":
if (typeof loaded == "function") loaded();
if (script.parentNode) script.parentNode.removeChild(script);
break;
}
}
script.onload = function() {
if (typeof loaded == "function") loaded();
if (script.parentNode) script.parentNode.removeChild(script);
}
script.onerror = function() {
if (typeof error == "function") error();
if (script.parentNode) script.parentNode.removeChild(script);
}
script.type = "text/javascript";
script.defer = "true";
script.src = url;
var parent = document.getElementsByTagName("HEAD")[0] || document.documentElement;
if (parent && parent.insertBefore) parent.insertBefore(script, parent.firstChild);
}
function button_translateClick() {
var word = encodeURIComponent($("text_word").value);
if (!word) {
alert('');
$("text_word").focus();
return;
}
callScript("http://server.dict-co.iciba.com/jsInterface.php?uiType=0&w=" + word + "&type=6");
callScript("http://server.dict-co.iciba.com/jsInterface.php?uiType=0&w=" + word + "&dict=Dict,Tf,Enen,");
callScript("http://server.dict-co.iciba.com/jsInterface.php?uiType=0&w=" + word + "&dict=Dict,Tf,Enen,");
callScript("http://server.dict-co.iciba.com/jsInterface.php?uiType=0&w=" + word + "&type=2");
}
function text_wordKeydown(e) {
if (!e) e = window.event;
switch (e.keyCode | e.which | e.charCode) {
case 13:
button_translateClick();
break;
}
}
function LoveCallback(context) {
$("div_context").innerHTML = context;
}
function dictCallBack(context) {
$("div_head").innerHTML = context;
}
function tfCallback(context) {
$("div_thesaurus").innerHTML = context;
}
function En2enCallback(context) {
$("div_en2en").innerHTML = context;
}
function djCallback(context) {
$("div_dj").innerHTML = context;
}
function searchDictByWord(e) {
var element = typeof event != "undefined" ? event.srcElement : e.target;
$("text_word").value = element.innerHTML;
button_translateClick();
}
</script>
<input id="text_word" type="text" value="hello" onkeydown="text_wordKeydown(event)" />
<input type="button" value="搜索" onclick="button_translateClick()"/>
<div id="div_head"></div>
<div id="div_context"></div>
<div id="div_thesaurus"></div>
<div id="div_en2en"></div>
<div id="div_dj"></div>
</body>
</html>
相關(guān)文章
JavaScript實(shí)現(xiàn)頁面跳轉(zhuǎn)的方式匯總
這篇文章主要介紹了JavaScript實(shí)現(xiàn)頁面跳轉(zhuǎn)的方式匯總的相關(guān)資料,需要的朋友可以參考下2016-05-05JavaScript實(shí)現(xiàn)移動端橫豎屏檢測
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)移動端橫豎屏檢測,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07JS實(shí)現(xiàn)分頁瀏覽橫向圖片(類輪播)實(shí)例代碼
這篇文章主要介紹了JS實(shí)現(xiàn)分頁瀏覽橫向圖片(類輪播)實(shí)例代碼,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11js獲取瀏覽器的可視區(qū)域尺寸的實(shí)現(xiàn)代碼
js獲取瀏覽器的可視區(qū)域尺寸的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-11-11分享Javascript中最常用的55個經(jīng)典小技巧
這篇文章主要介紹了Javascript中最常用的55個經(jīng)典小技巧。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11