首页 > 解决方案 > jquery根据同一行中的另一个值添加每个属性

问题描述

我有一个基于 python 数据框的动态表,并在第 2 列和第 23 列中添加了以下类:

 $(".tablafiltros tbody tr td:nth-child(2)").addClass('ColNodo');
 $(".tablafiltros tbody tr td:nth-child(23)").addClass('ColGest');

我还为 .ColGest 分配了以下属性:

        $(".ColGest").attr("data-pk","index")

通过这种方式,我将相同的固定属性“data-pk”=“index”添加到“.ColGest”中的每个,但我需要为每个“.ColGest”分配一个属性,该属性是 .ColNodo 的内容(单元格值)

提前致谢。

标签: jqueryhtml-tablecellattr

解决方案


// Loop over all .ColGest items
$('.ColGest').each(function(index) {

   // get the ColNodo cell with same index as the current ColGest in the loop
   const colNodoValue = $('.ColNodo')[index].text();

   // add a data-value attribute with the colNodo value
   $(this).attr("data-value", colNodoValue);

});


推荐阅读