2015-06-26 40 views
1

我正在使用此javascript來本地化我的Cordova/Phonegap應用程序。它依賴於我不想使用的JQuery。這最後一點特別是沒有工作刪除本地化代碼中的JQuery依賴項

 _results.push($(".l10n-" + key).html(value)); 

如何更改代碼,以消除該依賴關係? 謝謝

\t var Localization; 
 

 
\t Localization = (function() { 
 

 
\t function Localization() {} 
 

 
\t Localization.initialize = function(dictionnary, fallback_language) { 
 
\t \t Localization.dictionnary = dictionnary; 
 
\t \t Localization.fallback_language = fallback_language; 
 
\t \t return navigator.globalization.getPreferredLanguage(Localization.get_preferred_language_callback, Localization.get_preferred_language_error_callback); 
 
\t }; 
 

 
\t Localization.get_preferred_language_callback = function(language) { 
 
\t \t Localization.language = language.value; 
 
\t \t alert("Phone language is " + Localization.language); 
 
\t \t if (Localization.language in Localization.dictionnary) { 
 
\t \t alert("It is supported."); 
 
\t \t } else { 
 
\t \t Localization.language = Localization.fallback_language; 
 
\t \t alert("It is unsupported, so we chose " + Localization.language + " instead."); 
 
\t \t } 
 
\t \t return Localization.apply_to_current_html(); 
 
\t }; 
 

 
\t Localization.get_preferred_language_error_callback = function() { 
 
\t \t Localization.language = Localization.fallback_language; 
 
\t \t alert("There was a error determining the language, so we chose " + Localization.language + "."); 
 
\t \t return Localization.apply_to_current_html(); 
 
\t }; 
 

 
\t Localization.apply_to_current_html = function() { 
 
\t \t var key, value, _ref, _results; 
 
\t \t alert("Localizing HTML file."); 
 
\t \t _ref = Localization.dictionnary[Localization.language]; 
 
\t \t _results = []; 
 
\t \t for (key in _ref) { 
 
\t \t value = _ref[key]; 
 
\t \t alert(value); 
 
\t \t _results.push($(".l10n-" + key).html(value)); 
 
\t \t alert("result is " + _results); 
 
\t \t } 
 
\t \t return _results; 
 
\t }; 
 

 
\t Localization["for"] = function(key) { 
 
\t \t return Localization.dictionnary[Localization.language][key]; 
 
\t }; 
 

 
\t return Localization; 
 

 
\t })();

回答

0

您可以使用本機JS方法來改變這些元素的HTML:

Localization.apply_to_current_html = function() { 
    var key, value, _ref, _results; 
    _ref = Localization.dictionnary[Localization.language]; 
    _results = []; 
    for (key in _ref) { 
     value = _ref[key]; 
     var elements = document.querySelectorAll(".l10n-" + key); 
     for (var i = 0; i < elements.length; i++) { 
      elements[i].innerHTML = value; 
      _results.push(elements[i]); 
     } 
    } 
    return _results; 
}; 

注意的功能是相同的,但是apply_to_current_html()功能現在將返回一個DOMElement對象數組,而不像以前那樣是一個jQuery對象數組。

你還沒有說明如何使用這個返回值的例子,所以我不能告訴你這可能有什麼樣的連鎖效應。