我正在使用此代码开发一个使用多语言的网站,我让它完美地工作但是当它发生变化时我遇到了问题,文本中的所有样式都丢失了,顶级菜单不仅失去它的风格,但它也完全停止工作
这是更改文字的javascript函数:
(function() {
$.MultiLanguage = function(a, c) {
var b = this;
if (c == null) {
c = null
}
return $.getJSON(a, function(g) {
var f, d, h, e;
if (c !== null) {
localStorage.MultiLanguage = c
} else {
if (typeof localStorage.MultiLanguage === "undefined") {
c = localStorage.MultiLanguage = g.config["default"]
} else {
c = localStorage.MultiLanguage
}
}
d = g.language[c];
e = [];
for (f in d) {
h = d[f];
if ($(f).get(0).tagName.toLowerCase() === "title") {
document.title = h;
continue
}
if (f.length > 0) {
e.push($(f).html(d[f]))
} else {
e.push(void 0)
}
}
return e
})
}
})
.call(this);
JSON语言文件是这样的:
{
"config": {
"default": "es"
},
"language": {
"es": {
"#ml-home": "HOME",
"#ml-agencia": "AGENCIA",
"ml-img-serv":"images/labels/servicios.png",
"#ml-texto_agencia": "Somos una agencia creativa y multidisciplinaria que busca potenciar a sus clientes. "
},
"en": {
"#ml-home": "HOME",
"#ml-agencia": "AGENCY",
"ml-img-serv":"images/labels/agencia.png",
"#ml-texto_agencia": "Octagon is a creative and multidisciplinary agency seeking to empower its customers. "
}
}
}
答案 0 :(得分:0)
我发现为什么它会丢失样式,该函数会替换HTML中的所有内容,即使ID没有正确设置,也可以替换类。我修好了它,它运作正常。