Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JQuery在窗口中使用id进行翻译,以及从对象进行翻译?_Jquery - Fatal编程技术网

如何使用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行。这正是我要找的!