我正在使用此JavaScript来本地化我的Cordova / Phonegap应用。它依赖于我不想使用的JQuery。这里的最后一点特别是不起作用
_results.push($(".l10n-" + key).html(value));
如何更改代码以删除该依赖项? 谢谢
var Localization;
Localization = (function() {
function Localization() {}
Localization.initialize = function(dictionnary, fallback_language) {
Localization.dictionnary = dictionnary;
Localization.fallback_language = fallback_language;
return navigator.globalization.getPreferredLanguage(Localization.get_preferred_language_callback, Localization.get_preferred_language_error_callback);
};
Localization.get_preferred_language_callback = function(language) {
Localization.language = language.value;
alert("Phone language is " + Localization.language);
if (Localization.language in Localization.dictionnary) {
alert("It is supported.");
} else {
Localization.language = Localization.fallback_language;
alert("It is unsupported, so we chose " + Localization.language + " instead.");
}
return Localization.apply_to_current_html();
};
Localization.get_preferred_language_error_callback = function() {
Localization.language = Localization.fallback_language;
alert("There was a error determining the language, so we chose " + Localization.language + ".");
return Localization.apply_to_current_html();
};
Localization.apply_to_current_html = function() {
var key, value, _ref, _results;
alert("Localizing HTML file.");
_ref = Localization.dictionnary[Localization.language];
_results = [];
for (key in _ref) {
value = _ref[key];
alert(value);
_results.push($(".l10n-" + key).html(value));
alert("result is " + _results);
}
return _results;
};
Localization["for"] = function(key) {
return Localization.dictionnary[Localization.language][key];
};
return Localization;
})();
答案 0 :(得分: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对象数组。
您没有说明如何使用此返回值的示例,因此我无法告诉您这可能产生的连锁效应。