如何使用JQuery在窗口中使用id进行翻译,以及从对象进行翻译?
我在如何使用JQuery在窗口中使用id进行翻译,以及从对象进行翻译?,jquery,Jquery,我在中有一个id为的透视表,我想知道如何使用这些id来获得翻译 用这个 $('th').each(function(){ if ($(this).text() == '1234') { $(this).text('MyTranslationWithId1234'); } }); 我的目标是: {1234: 'text1234'}, {3232: 'text2332'}, {3278: 'text3278'} 我在中有大约500个id,我想使用JQuery直接翻译它们这似乎是一种奇怪
中有一个id为的透视表,我想知道如何使用这些id来获得翻译
用这个
$('th').each(function(){
if ($(this).text() == '1234') { $(this).text('MyTranslationWithId1234'); }
});
我的目标是:
{1234: 'text1234'},
{3232: 'text2332'},
{3278: 'text3278'}
我在
中有大约500个id,我想使用JQuery直接翻译它们这似乎是一种奇怪的方式,我只使用一个带键的对象,而不是多个对象,如下所示:
var translation = {
1234: 'text1234',
3232: 'text2332',
3278: 'text3278'
};
$('th').text(function(_,txt){ return translation[parseInt(txt,10)]; });
否则,您将不得不进行大量缓慢的迭代:
var translation = [
{1234: 'text1234'},
{3232: 'text2332'},
{3278: 'text3278'}
];
$('th').text(function(_,txt){
var key = parseInt(txt,10);
$.each(translation, function(_, obj) {
if ( key in obj) txt = obj[key];
});
return txt;
});
是一个列标题。你真的有500列吗?请发布一个你的HTMLY的例子,它是一个巨大的矩阵,有时使用500列2000行。这正是我要找的!