Help with the script:
There are 2 tables with different classes ( desktop-table and mobile-table ), but the cells have the same classes. Implemented as follows:

 $('.price-table.desktop-table td').each(function(key, value){ $(this).attr('class', 'cell-blok-'+key); }); $('.price-table.mobile-table td').each(function(key, value){ $(this).attr('class', 'cell-blok-'+key); }); 

How can JQuery move the entire nested context between them?

  • Make a replicable example. If you answer a specific question: you can. - ilyaplot

1 answer 1

It depends on what is meant by this (nested context). If the assignment of attributes, classes and other things, then it is better to put a separate function into the function:

 jQuery.fn.extend({ fillData: function(mode = 'desktop'){ /* Здесь будет общая часть и для мобильных, и для десктопных */ $(this).attr('class', 'cell-blok-'+key); //например if(mode == 'desktop'){ $(this).toggleClass('desktop');//для десктопа return this; } /* Здесь будет обработка мобильного контента */ return this;//возвращаем себя, чтобы можно было использовать chain-function } }) $('.price-table').each(function(){ var mode = $(this).hasClass('mobile-table');//Проверяем в какой мы таблице mode = mode ? 'mobile' : 'desktop';//тернарный оператор на проверку и присвоение значения mode $(this).find('td').each(function(){ $(this).fillData(mode);//заполнение аттрибутов ячейки }) })