jquery - 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 的内容(单元格值)
提前致谢。
解决方案
// 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);
});
推荐阅读
- r - 使用 stringr 将下划线分隔的两个单词的首字母大写
- android - 通过 Apollo 进行 GraphQL 查询
- python - 我应该为我的应用程序使用 SQLite 数据库还是 Pandas
- java - Spring Reactor:带有 onErrorContinue 的 switchIfEmpty - 错误还是功能?
- java - 如何调试第三方 Intellij IDEA 插件?
- python - Python Pandas:最后一行如何在组内及时关闭?
- ruby-on-rails - 组合相同的嵌套属性
- npm - npm audit - 如何确保我没有生产漏洞?
- regex - 在 Sublime Text 3 中创建一个不贪婪的正则表达式
- dictionary - 无效的过程调用或参数错误 vb 6