如何为多个动态文本框启用Google音译(ASP.Net)
本文关键字:音译 Google ASP Net 启用 文本 动态 | 更新日期: 2023-09-27 18:16:43
这是在ASP中集成Google音译代码的示例代码(随处可见)。网络页面。
但是我的问题是,如何在运行时生成的文本框中启用音译?该脚本需要用于应用音译的文本框的ID。但我的文本框将在运行时生成。
需要这行代码的替代:
control.makeTransliteratable ([' transliterateTextarea ']);
//Script Starts here
// Load the Google Transliterate API
google.load("elements", "1", {
packages: "transliteration"
});
function onLoad() {
var options = {
sourceLanguage:
google.elements.transliteration.LanguageCode.ENGLISH,
destinationLanguage:
[google.elements.transliteration.LanguageCode.HINDI],
shortcutKey: 'ctrl+g',
transliterationEnabled: true
};
// Create an instance on TransliterationControl with the required
// options.
var control =
new google.elements.transliteration.TransliterationControl(options);
// Enable transliteration in the textbox with id
// 'transliterateTextarea'.
control.makeTransliteratable(['transliterateTextarea']);
}
google.setOnLoadCallback(onLoad);
//End here
使用RegisterStartupScript。RegisterStartupScript将在页面加载完成后执行。
function EnableTransalation(ctrlId) {
//Script Starts here
// Load the Google Transliterate API
google.load('elements', '1', {
packages: 'transliteration'
});
function onLoad() {
var options = {
sourceLanguage:
google.elements.transliteration.LanguageCode.ENGLISH,
destinationLanguage:
[google.elements.transliteration.LanguageCode.HINDI],
shortcutKey: 'ctrl+g',
transliterationEnabled: true
};
// Create an instance on TransliterationControl with the required
// options.
var control =
new google.elements.transliteration.TransliterationControl(options);
// Enable transliteration in the textbox with id
// 'transliterateTextarea'.
control.makeTransliteratable(["'" + ctrlId + "'"]);
}
google.setOnLoadCallback(onLoad);
//End here
}
后面的代码中,
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterStartupScript(GetType(), "EnableTransalation", "EnableTransalation('" + ctrl.ClientID + "')", true);
}
首先你必须设置所有文本框的类名为hindiFont。
使用此代码:
google.load("elements", "1", {
packages: "transliteration"
});
function onLoad() {
var options = {
sourceLanguage: [google.elements.transliteration.LanguageCode.ENGLISH],
destinationLanguage: [google.elements.transliteration.LanguageCode.HINDI],
transliterationEnabled: true,
shortcutKey: 'ctrl+g'
};
var control = new google.elements.transliteration.TransliterationControl(options);
$('.hindiFont').each(function(){
var id = this.id;
control.makeTransliteratable([id]);
})
}
google.setOnLoadCallback(onLoad);