如何在网页代码里使用JS实现谷歌浏览器自动翻译

JavaScript085

如何在网页代码里使用JS实现谷歌浏览器自动翻译,第1张

Google 翻译网页功能可以在包括中文和英文的35种语言之间实现整个网页的翻译。当你点击此按钮时,它会自动识别当前网页的语言,把它的内容上传到Google的翻译服务器翻译成用户浏览器设置的语言,,再将翻译结果用原有的网页格式显示出来。这时,该按钮的文字显示会变成“原始网页“,并且会在网页最上端出现一个信息条。如果你想把网页译成其它语言,你可以在信息条上改动翻译语言。另外,你如果点击已翻译网页上的任何链接,并且链接的网页的语言非浏览器设置语言的话,该功能会自动翻译链接网页。你可以点击“原始网页“或者点击信息条上的“X”键关掉信息条,网页就会回到翻译前的状态。

另外,Google工具栏还有字词翻译和划词翻译的功能。“翻译网页”图标旁的下拉菜单会显示两种功能的设置,即字词翻译和划词翻译。启用字词翻译功能后,将光标悬停在一个字词上,就能查看其翻译。若启用划词翻译,当你用鼠标高亮一串单词时,会在这些文字旁出现对整串文字的翻译

用javascript写的,望采纳

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

</head>

<body>

<p>

<span class="en">The heaven being spread with this pallid screen and the earth with the darkest vegetation, their meeting-line at the horizon was clearly marked. </span>

<span class="cn" style="display: none">蒙住苍穹的是这片灰白的帐幕,遍布大地的是这片黑苍苍的石南植物,它们在天际处交接,呈现出一条清晰分明的界线。</span>

<span class="en">In such contrast the heath wore the appearance of an instalment of night which had taken up its place before its astronomical hour was come: darkness had to a great extent arrived hereon, while day stood distinct in the sky. </span>

<span class="cn" style="display: none">在如此鲜明的对比下,这片荒原不等夜晚按天时自然降临,便早早蒙上了一层夜色;也就是说,在大地已经显出一片黑苍苍之际,天空依然清晰可见,昼光未逝。</span>

<span class="en">Looking upwards, a furze-cutter would have been inclined to continue worklooking down, he would have decided to finish his faggot and go home. </span>

<span class="cn" style="display: none">抬头望天,一位砍荆条的人会想要继续手头的活儿,然而低头凝望,他便会决定捆好柴束,打道回府。

</p>

</body>

<script>

var enarr = document.getElementsByClassName("en")

for(var i=0i<enarr.lengthi++){

enarr[i].onclick = function(obj){

return function(){

if(obj.nextElementSibling.style.display=='none'){

obj.nextElementSibling.style.display=''

} else {

obj.nextElementSibling.style.display='none'

}

}

}(enarr[i])

}

</script>

</html>