简体   繁体   English

如何在运行时从用户获取此字符串?

[英]How to take this string from user at run time?

Look at the below code, this JavaScript is used to take a string (in a language other than English) and convert it into English. 看下面的代码,此JavaScript用于获取字符串(英语以外的语言)并将其转换为英语。

<script type="text/javascript">
    google.load("language", "1");

    function initialize() {
       var content = document.getElementById('translation');
       // Setting the text in the div.
       content.innerHTML = '<div id="text">HELLO WORLD<\/div>
                            <div id="translation"/>';

       // Grabbing the text to translate
       var text = document.getElementById("text").innerHTML;

       // Translate from Spanish to English, and have the callback of
       // the request put the resulting translation in the
       // "translation" div.  Note: by putting in an empty string for
       // the source language ('es') then the translation will
       // auto-detect the source language.

       google.language.translate(text, '', 'en', function(result) {
       var translated = document.getElementById("translation");
       if (result.translation) {
           translated.innerHTML = result.translation;
       }
     });
   }

   google.setOnLoadCallback(initialize);

</script>

I want that the string "HELLO WORLD" must be entered by user at run time in a text field and then that string is passed to the div id text. 我希望用户在运行时必须在文本字段中输入字符串“ HELLO WORLD”,然后将该字符串传递给div id文本。 So is this possible? 那有可能吗?

Hope you are referring to the document below: 希望您参考以下文档:

http://code.google.com/apis/language/translate/v1/getting_started.html http://code.google.com/apis/language/translate/v1/getting_started.html

Please refer to the section "Getting Started" where it says about "Signing up for an API key". 请参阅“入门”部分中有关“注册API密钥”的内容。 This needs to be done before you could implement the code in your page. 必须先完成此操作,然后才能在页面中实现代码。

Once done, make the modification to the script file which you include in the html page with your key. 完成后,用您的密钥对包含在html页面中的脚本文件进行修改。

Here, replace your key with "MY_KEY_STRING" in the bottom code and get started. 在这里,用底部代码中的“ MY_KEY_STRING”替换密钥,然后开始使用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google AJAX Language API Sample</title>
    <script src="https://www.google.com/jsapi?key=MY_KEY_STRING"></script>
    <script type="text/javascript">

    google.load("language", "1");

    function initialize() {
    //Show the translate button
    document.getElementById("translateButton").style.display = "";
     }
    google.setOnLoadCallback(initialize);
    function translate() {
    var text = document.getElementById("fromText").value;
        google.language.translate(text, 'es', 'en', function(result) {
        var translated = document.getElementById("toText");
        if (result.translation) {
          translated.innerHTML = result.translation;
        }
      });
    }
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    From:<input type="text" id="fromText"/>
    To:<span id="toText"></span>
    <input type="button" value="Translate" onclick="translate()" style="display: none;" id="translateButton">
  </body>
</html>

HTML: HTML:

<form id="translate">
  <textarea id="translate-me"></textarea>
  <input type="submit" />
</form>

JavaScript: JavaScript:

var form = document.getElementById('translate')
var textarea = document.getElementById('translate-me')

form.onsubmit = function () {
  google.language.translate(textarea.value, ...)
  return false; // prevent default action (form submission)
}

Using jQuery or something similar would make this easier, of course. 当然,使用jQuery或类似的东西会使它更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM