代码肯定是可以优化的,只是写的少,懒得写了。
<!doctype html>
<head>
<meta charset="utf-8"/>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<h1>只提供查询日语</h1>
请输入你要查询的单词:<input id="danci" ><button onclick="search()">查询</button>
<div>
<textarea name="" id="content" cols="30" rows="10"></textarea>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="./md5.js"></script>
<script type="text/javascript">
var appid = ''; //把自己申请的appid写进去
var key = ''; //key也一样
var salt = (new Date).getTime();
function search(){
var query = $("#danci").val();
console.log(query);
// 多个query可以用\n连接 如 query='apple\norange\nbanana\npear'
var from = 'zh';
var to = 'jp';
var str1 = appid + query + salt +key;
var sign = MD5(str1);
$.ajax({
url: 'http://api.fanyi.baidu.com/api/trans/vip/translate',
type: 'get',
dataType: 'jsonp',
data: {
q: query,
appid: appid,
salt: salt,
from: from,
to: to,
sign: sign
},
success: function (data) {
console.log(data.trans_result[0].dst);
let japen=data.trans_result[0].dst;
$("#content").text(japen)
}
});
}
function searchEnglish(){
var query = $("#danciEnglish").val();
console.log(query);
// 多个query可以用\n连接 如 query='apple\norange\nbanana\npear'
var from = 'zh';
var to = 'en';
var str1 = appid + query + salt +key;
var sign = MD5(str1);
$.ajax({
url: 'http://api.fanyi.baidu.com/api/trans/vip/translate',
type: 'get',
dataType: 'jsonp',
data: {
q: query,
appid: appid,
salt: salt,
from: from,
to: to,
sign: sign
},
success: function (data) {
console.log(data.trans_result[0].dst);
let japen=data.trans_result[0].dst;
$("#englishContent").text(japen)
}
});
}
function searchAll(){
searchJapen();
setTimeout(function(){ searchEnglish();},1000)
}
</script>
</body>
网站是这个https://api.fanyi.baidu.com/doc/21
转语音的是百度智能云网站 别记错了
https://cloud.baidu.com/search.html?q=%E8%AF%AD%E9%9F%B3%E5%90%88%E6%88%90&type=&pn=1
|