为什么我的Bing Translate代码在除Google Chrome之外的所有浏览器中都能完美运行?

时间:2012-11-05 09:20:28

标签: javascript api bing

我有一个简单的Bing Translate示例,但它不适用于Google Chrome。

我不明白这是一个bing问题还是我的代码错了。

这是我的例子的代码

<!doctype html>
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">  
   function translate() {
   document.getElementById('trans').innerHTML="";
   var text= document.getElementById('ori').value;
   window.mycallback = function(response) {
   document.getElementById('trans').innerHTML=response;
   }

   var languageFrom = document.getElementById("langpairFROM").value;
   var languageTo = document.getElementById("langpairTO").value;
   var s = document.createElement("script");
   s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback&appId=68D088969D79A8B23AF8585CC83EBA2A05A97651&from=" + languageFrom + "&to=" + languageTo + "&text=" + text;
document.getElementsByTagName("head")[0].appendChild(s);
   }
</script>
</head>

<body>
<center>


<select id="langpairFROM" STYLE=" font-size : 11pt">
  <option value="de">German</option>
  <option value="en">English</option>
  <option value="ru">Russian</option>                                         
</select>

<select id="langpairTO" STYLE=" font-size : 11pt">
  <option value="ru">Russian</option>
  <option value="de">German</option>
  <option value="en">English</option>  
</select>

<button type="input" onclick="translate()" >Translate</button> 

<div >
  <textarea id="ori" name="translate" ></textarea> 
</div>

<div id="translationField">
  <textarea id="trans" >Translated Text</textarea> 
</div
</center>
</body> 

</html> 

Here ist the jsfiddle example

2 个答案:

答案 0 :(得分:2)

因为您还有一个名为<textarea>的{​​{1}}。

更改功能名称或translate名称:

JSFidde

PS。我假设您使用<textarea>之类的东西来“清理”生产代码中的网址。

答案 1 :(得分:0)

我修改了你的代码。它现在适用于Chrome

<!doctype html>
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">  
  function translate() {
  document.getElementById('translate_to').innerHTML="";
  var text= document.getElementById('translate_from').value;
  window.mycallback = function(response) {
  document.getElementById('translate_to').innerHTML=response;
}

var languageFrom = document.getElementById("langpairFROM").value;
var languageTo = document.getElementById("langpairTO").value;
var s = document.createElement("script");
s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback&appId=68D088969D79A8B23AF8585CC83EBA2A05A97651&from=" + languageFrom + "&to=" + languageTo + "&text=" + text;
document.getElementsByTagName("head")[0].appendChild(s);
}
</script>
</head>
<body>
<form action="javascript:translate()">
<center>
<select id="langpairFROM" STYLE=" font-size : 11pt">
  <option value="de">German</option>
  <option value="en" selected>English</option>
  <option value="ru">Russian</option>                                         
</select>
<select id="langpairTO" STYLE=" font-size : 11pt">
  <option value="ru">Russian</option>
  <option value="de" selected>German</option>
  <option value="en">English</option>  
</select>
<button type="submit">Translate</button> 
<div >
  <textarea id="translate_from" name="translate_from" ></textarea> 
</div>
<div id="translationField">
  <textarea id="translate_to" >Translated Text</textarea> 
</div
</center>
</form>
</body> 
</html>