gpt4 book ai didi

javascript - 如何使用 javascript/jquery 循环遍历表?

转载 作者:行者123 更新时间:2023-11-28 11:22:32 25 4
gpt4 key购买 nike

我有如下代码块

<tbody class="society_list">
<tr>
<td>1</td>
<td>Dummy</td>
<td>Dummy</td>
<td id="lol0">UPDATE THIS</td>
</tr>
<tr>
.....
</tr>
</tbody>

我想做的是循环整个表,找到带有id的td,获取该id的值,然后更新里面的html。我现在拥有的(抱歉,我很新,我仍然不知道该怎么做......)

function update(){
var trs = document.querySelectorAll('.society_list tr');
for(i=0;i<trs.length-1;i++){
trs[i].find('td').each(function(){
//I know I need to do something here but what's that..
});
}
}

最佳答案

使用 has attribute selector 迭代具有 id 属性的 tds .

$('.society_list tr td[id]').each(function(){
var tdID = $(this).attr('id'); // <--- getting the ID here
var result = doSomeMagicWithId(tdID); // <--- doing something
$(this).html(result); // <---- updating the HTML inside the td
});

关于javascript - 如何使用 javascript/jquery 循环遍历表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com