Google AJAX Transliteration API:是否可以使页面中的所有输入字段都可音译?

发布于 2024-08-28 23:21:57 字数 463 浏览 8 评论 0原文

我使用过“Google AJAX Transliteration API”,它对我来说很顺利。

http://code.google.com/apis/ajaxlanguage/documentation/referenceTransliteration。目前

我有一个项目,我需要每个页面中的所有输入字段(输入和文本区域标签)都是可音译的,而这些输入字段因页面而异(动态)。 据我所知,我必须在 API 调用中调用 makeTransliteratable(elementIds, opt_options) 方法来定义哪些输入字段可转写,在我的例子中,我无法手动预定义这些字段。有办法实现这一点吗?

提前致谢

I've used "Google AJAX Transliteration API" and it's going well with me.

http://code.google.com/apis/ajaxlanguage/documentation/referenceTransliteration.html

Currently I've a project that I need all input fields in every page (input & textarea tags) to be transliteratable, while these input fields differs from page to page (dynamic).
As I know, I've to call makeTransliteratable(elementIds, opt_options) method in the API call to define which input fields to make transliteratable, and in my case here I can't predefine those fields manually. Is there a way to achieve this?

Thanks in advance

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

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

发布评论

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

评论(1

空城旧梦 2024-09-04 23:21:57

重新表述您的要求:您希望将页面上符合特定条件的所有输入收集在一起,然后将它们传递到 api 中。

快速浏览一下 API 参考,发现 makeTransliteratable 将接受 id 字符串数组或元素数组。由于我们事先不知道元素的 id,因此我们将传递一个元素数组。

那么,如何获取数组的元素呢?

我将向您展示两种方法:一种困难的方法和一种简单的方法。

首先,要获取所有文本区域,我们可以使用 document.getElementsByTagName API 来实现:

var textareas = document.getElementsByTagName("textarea");

获取输入列表稍微困难一些,因为我们不想包含复选框、单选按钮等。我们可以通过它们的不同来区分它们type 属性,所以让我们编写一个快速函数来进行区分:

function selectElementsWithTypeAttribute(elements, type)
{
    var results = [];
    for (var i = 0; i < elements.length; i++)
    {
        if (elements[i].getAttribute("type") == type)
        {
            results.push(elements[i]);
        }
    }
    return results;
}

现在我们可以使用这个函数来获取输入,如下所示:

var inputs = document.getElementsByTagName("input")
var textInputs = selectElementsWithTypeAttribute(textInputs, "text");

现在我们已经引用了所有文本框,我们可以将它们连接到一个数组中,然后传递到 api:

var allTextBoxes = [].concat(textareas).concat(textInputs);
makeTransliteratable(allTextBoxes, /* options here */);

所以,这应该都可以工作,但是我们可以通过明智地使用库方法来使其变得更容易。如果您要下载 jQuery(google 一下),那么您可以编写以下更紧凑的代码:

var allTextBoxes = $("input[type='text'], textarea").toArray();
makeTransliteratable(allTextBoxes, /* options here */);

这使用 CSS 选择器来查找类型属性为“text”的所有输入以及所有文本区域。有一个方便的 toArray 方法,它将所有输入放入一个数组中,准备传递给 makeTransliteratable。

我希望这有帮助,
道格拉斯

Rephrasing what you are asking for: you would like to collect together all the inputs on the page which match a certain criteria, and then pass them into an api.

A quick look at the API reference says that makeTransliteratable will accept an array of id strings or an array of elements. Since we don't know the ids of the elements before hand, we shall pass an array of elements.

So, how to get the array of elements?

I'll show you two ways: a hard way and an easy way.

First, to get all of the text areas, we can do that using the document.getElementsByTagName API:

var textareas = document.getElementsByTagName("textarea");

Getting the list of inputs is slightly harder, since we don't want to include checkboxes, radio buttons etc. We can distinguish them by their type attribute, so lets write a quick function to make that distinction:

function selectElementsWithTypeAttribute(elements, type)
{
    var results = [];
    for (var i = 0; i < elements.length; i++)
    {
        if (elements[i].getAttribute("type") == type)
        {
            results.push(elements[i]);
        }
    }
    return results;
}

Now we can use this function to get the inputs, like this:

var inputs = document.getElementsByTagName("input")
var textInputs = selectElementsWithTypeAttribute(textInputs, "text");

Now that we have references to all of the text boxes, we can concatenate them into one array, and pass that to the api:

var allTextBoxes = [].concat(textareas).concat(textInputs);
makeTransliteratable(allTextBoxes, /* options here */);

So, this should all work, but we can make it easier with judicious use of library methods. If you were to download jQuery (google it), then you could write this more compact code instead:

var allTextBoxes = $("input[type='text'], textarea").toArray();
makeTransliteratable(allTextBoxes, /* options here */);

This uses a CSS selector to find all of the inputs with a type attribute of "text", and all textareas. There is a handy toArray method which puts all of the inputs into an array, ready to pass to makeTransliteratable.

I hope this helped,
Douglas

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