我有一些代码,当我在文本框中输入一些测试并单击Tranlate时,它将翻译成英语:
<html>
<head>
<style type="text/css">
input {font-size:12px; width:600px;}
</style>
<title>Translate</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div class="translate"></p>
<div class="translate_control" lang="en">
<input id="text1" class="translate"/>
<script>
function googleSectionalElementInit() {
new google.translate.SectionalElement({
sectionalNodeClassName: 'translate',
controlNodeClassName: 'translate_control',
background: '#f4fa58'
}, 'google_sectional_element');
}
</script>
<script src="http://translate.google.com/translate_a/element.js?cb=googleSectionalElementInit&ug=section&hl=en"></script>
</body>
</html>现在,我希望将一些代码输入到text1中:\u83B7\u53D6\u9875\u9762,当我单击“翻译”时,它将自动使用javascript text1()到获取页面,并使用谷歌翻译自动翻译成英语,然后显示到text1。该怎么做呢?
抱歉,我的英语!
发布于 2014-05-20 15:03:48
这样做可能有一种更短的方法,但有一种选择是使用JSON预定义的类来处理所有转义。encodeURI/decodeURI听起来似乎是最明显的方法,但它们都不符合您所尝试的方向,即接受一个字符串文字,它实际上是"\u83B7\u53D6\u9875\u9762",包括文字反斜杠。您想要的实际上是重新评估输入字符串。
我就这样做了:
var escapedInput = document.querySelector("#text1").value;
var encodedChars = JSON.parse("\"" + escapedInput + "\"");现在我的encodedChars是获取页面。把这个交给你的翻译。
https://stackoverflow.com/questions/23761813
复制相似问题