javascript – 如何根据Class not Id进行Google音译

前端之家收集整理的这篇文章主要介绍了javascript – 如何根据Class not Id进行Google音译前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在做一个基于泰米尔语言的Web应用程序.在我的应用程序中,我使用动态字段添加用户详细信息.那么,动态字段有多个ID如何执行此操作或如何使用基于类的Google Transliteration?
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
  // Load the Google Transliteration API
  google.load("elements","1",{
    packages: "transliteration"
  });

  function onLoad() {
    var options = {
      sourceLanguage: 'en',destinationLanguage: 'ta',shortcutKey: 'ctrl+m',transliterationEnabled: true
    };

    // Create an instance on TransliterationControl with the required
    var control = new google.elements.transliteration.TransliterationControl(options);

    // Enable transliteration in the textfields with the given ids.
    var ids = ['temrorary_address','permanant_address','bankbranch','member_name','father_husband','workingoffice_address',];
    control.makeTransliteratable(ids);

    // Show the transliteration control which can be used to toggle between
    // English and Hindi and also choose other destination language.
    control.showControl('translControl');
  }
  google.setOnLoadCallback(onLoad);
</script>

解决方法

@H_301_7@ makeTransliteratable输入不仅可以是id数组,还可以是元素引用.
所以你可以写:
// Enable transliteration in the textfields with the given Class.
var elements = document.getElementsByClassName('Type-Your-Class-Here');
control.makeTransliteratable(elements);

但是如果你动态添加字段,它就无济于事.对于动态添加的字段,每次添加新字段时都必须调用control.makeTransliteratable.

原文链接:https://www.f2er.com/js/158051.html

猜你在找的JavaScript相关文章