Google翻译javascript代码段不起作用 [英] Google Translate javascript snippet not working
本文介绍了Google翻译javascript代码段不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用w3school.com的代码段.它可以在w3school上运行,但不能在我的PC上运行.
I tried using the code snippet from w3school.com. It worked on w3school but doesnt work on my PC.
<div id="google_translate_element"></div>
<script>
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en'
}, 'google_translate_element');
}
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
我在控制台中得到了以下内容.
I got the following in the console.
translate.html:18 GET file://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit net::ERR_FILE_NOT_FOUND
推荐答案
位于 w3school的摘录确实有一个错误.它说要添加以下行以包含Google的API:
The snippet over at w3school indeed has a bug. It says to add the following line to include Google's API:
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
不幸的是,结尾的//使其指向本地文件.因此,除非您已下载该库并将其与html文件捆绑在一起,否则这将无处可寻.而是通过添加 https:
Unfortunately the trailing // makes it point to a local file. So unless you've downloaded the library and bundled it with your html file this points to nowhere. Instead link to the online library by adding https:
<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
这篇关于Google翻译javascript代码段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文