js实现中英文切换
1.html
<!DOCTYPE html>
<head>
<title>中英文切换</title>
<meta charset="UTF-8" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/language.js"></script>
</head>
<body>
<button id="change">中英文切换</button>
<div style="text-align: center">
你好
</div>
</body>
<script type="text/javascript">
$("#change").click(function() {
translate();
})
</script>
</html>
2.language.js
$(function(){
// do something
var script=document.createElement("script");
script.type="text/javascript";
script.src="js/translate.js";
document.getElementsByTagName('head')[0].appendChild(script);
var value = sessionStorage.getItem("language");
document.onreadystatechange = function () {
if (document.readyState == 'complete') {
if(value==="1"){
Microsoft.Translator.Widget.Translate('zh-CHS', 'en', onProgress, onError, onComplete, onRestoreOriginal, 2000);
}
}
}
function onProgress(value) {
}
function onError(error) {
}
function onComplete() {
$("#WidgetFloaterPanels").hide();
}
function onRestoreOriginal() {
}
});
function translate(){
var value = sessionStorage.getItem("language");
if(value==="1"){
sessionStorage.setItem("language", "0");
}else{
sessionStorage.setItem("language", "1");
}
window.location.reload();//刷新当前页面.
}
3.translate.js是将http://www.microsoftTranslator.com/ajax/v3/WidgetV3.ashx?siteData=ueOIGRSKkd965FeEGM5JtQ**的源码下载下来的,并将里面
<1> J = "block", ib = "inline-block",都改成等于none
<2>s.style.color = q;s.style.backgroundColor = p两行注释
目的是去除翻译后出现的弹框和hover效果
最后:
转载自原文链接, 如需删除请联系管理员。
原文链接:js实现中英文切换,转载请注明来源!