如何在运行时从用户那里获取该字符串?

发布于 2024-12-11 07:58:39 字数 1200 浏览 0 评论 0原文

看下面的代码,这段 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>

我希望用户必须在运行时在文本字段中输入字符串“HELLO WORLD”,然后将该字符串传递给 div id 文本。那么这可能吗?

Look at the below code, this JavaScript is used to take a string (in a language other than English) and convert it into English.

<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. So is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你怎么这么可爱啊 2024-12-18 07:58:39

希望您参考以下文档:

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

请参阅“入门”部分,其中介绍了“注册 API 密钥”。需要先完成此操作,然后才能在页面中实现代码。

完成后,使用密钥修改 html 页面中包含的脚本文件。

在这里,将底部代码中的密钥替换为“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>

Hope you are referring to the document below:

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". 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.

Here, replace your key with "MY_KEY_STRING" in the bottom code and get started.

<!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>
池木 2024-12-18 07:58:39

HTML:

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

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)
}

当然,使用 jQuery 或类似的东西会让这变得更容易。

HTML:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文