google API translate, only a div into page

前端 未结 2 1270
北海茫月
北海茫月 2021-01-13 14:26

I have an HTML page, and I would use Google Translate to translate only a div into my page.

2条回答
  •  死守一世寂寞
    2021-01-13 15:08

    Check the examples at Google AJAX Language API.

    google.language.translate("Hello world", "en", "es", function(result) {
      if (!result.error) {
        var container = document.getElementById("translation");
        container.innerHTML = result.translation;
      }
    });
    

    use innerHTML to get hold of the contents of your DIV. You may have to strip out formatting first, though.

提交回复
热议问题