0

我有一个带有 id 的数据透视表,<TH>我想知道如何使用这些 id 进行翻译。

使用这个

$('th').each(function(){
    if ($(this).text() == '1234') { $(this).text('MyTranslationWithId1234'); }
}); 

我的对象是这样的:

{1234: 'text1234'},
{3232: 'text2332'},
{3278: 'text3278'}

我有大约 500 个 id <th>,我想直接使用 JQuery 翻译它们

4

1 回答 1

1

似乎是一种奇怪的方法,我只会使用一个带有键的对象,而不是多个对象,如下所示:

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;
});
于 2013-08-05T21:45:05.957 回答