我有一个包含 3 行的表,我试图循环遍历该表并获取表每一行中链接的标题属性,将其分配给一个变量并将其放入一个函数中。下面是我正在使用的代码:
$('tr').each(function(){
//$('.distance').html("hello");
//nodelat = $(this).find('.latitude').html();
//nodelon = $(this).find('.longitude').html();
postcode = $(this+".location a").attr('title');
console.log(postcode);
//$(this).find('.distance').html(distance(startlat, startlon, nodelat,nodelon));
});
出于某种原因,它似乎只是将第一个表行的值重复 3 次。我错过了什么吗?
该表的代码是:
<table id="locations" class="tablesorter">
<tbody>
<tr>
<td>Name</td>
<td>Distance</td>
</tr>
<tr>
<td class="location"><a href="#Dockyard" onclick="getLatLng("m502st", "This is a pub")" title="m502st">Dockyard (show on map)</a></td>
<td class="distance"></td>
</tr>
<tr>
<td class="location"><a href="#Joseph Holt" onclick="getLatLng("M3 1JD", "http://www.joseph-holt.com")" title="M3 1JD">Joseph Holt (show on map)</a></td>
<td class="distance"></td>
</tr>
</tbody>
</table>
最佳答案
更改:
$(this+".location a").attr('title');
致:
$(this).children('.location').children('a').attr('title');
或者:
$(this).find('.location > a').attr('title');
关于javascript - jQuery 表格的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23565745/