以编程方式更改Google翻译下拉列表 [英] Change Google Translate dropdown programmatically
问题描述
在网站上,我尝试使用以下代码添加Google翻译下拉列表:
On a site I tried adding the Google Translate dropdown using the following code:
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en'
}, 'google_translate_element');
}
<script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
当您从谷歌脚本插入的下拉列表中选择时,Google翻译栏会显示在顶部页面,所有文本都被翻译成所选语言。
When you select from the dropdown that the google script inserts, a Google Translate bar appears at the top of the page, and all text is translated in to the selected language.
但是,如果我尝试使用JavaScript触发下拉列表更改,则不起作用:
However if I try and trigger the dropdown change using JavaScript, it doesn't work:
$('.goog-te-combo').val('fr')
从下拉列表中选择法语,但不会触发Google翻译。
'French' is selected from the dropdown, however Google Translate is not triggered.
为什么o为什么不起作用?我也尝试过:
Why o why does it not work? I've also tried:
$('.goog-te-combo').trigger('click')
$('.goog-te-combo').change()
UPDATE:FYI this不是我的网站。我使用Chrome控制台加载jQuery并执行jQuery方法。
UPDATE: FYI this is not my site. I was using the Chrome console to load jQuery and execute the jQuery methods.
推荐答案
您可以让您的下拉菜单触发页面重新加载。您可以使用 #googtrans(en | ja)
或#googtrans / en / ja
重新加载页面。 URL,或者在重新加载之前将googtrans cookie值设置为 / en / ja
(其中ja是所选目标语言的示例)。
You can have your dropdown trigger a page reload. You can either reload the page with #googtrans(en|ja)
or #googtrans/en/ja
appended to the URL, or set the googtrans cookie value to /en/ja
(where ja is an example of the selected target language) before reloading.
这篇关于以编程方式更改Google翻译下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!