javascript - jQuery 表格的每一行

标签 javascript jquery html

我有一个包含 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(&quot;m502st&quot;, &quot;This is a pub&quot;)" title="m502st">Dockyard (show on map)</a></td>
      <td class="distance"></td>
    </tr>
    <tr>
      <td class="location"><a href="#Joseph Holt" onclick="getLatLng(&quot;M3 1JD&quot;, &quot;http://www.joseph-holt.com&quot;)" 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');

JSFiddle demo .

关于javascript - jQuery 表格的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23565745/

相关文章:

javascript - Angular2 和 ionic2 中的 Observable 和 Promise 请求失败

javascript - 如何将对象的属性和值映射到表?

javascript - 如何在 Chartjs 2.x 中使用图表插件 (Chart.pluginService.register) 访问标签数组?

javascript - 按钮中 onClick 绑定(bind)的正确方法

javascript - 基于下拉选择的日期选择器的两种不同文本输入

javascript - 如何自动注入(inject)状态参数

jQuery - 在加载时添加按钮样式,并在单击其他按钮时删除样式。

java - 从 Itext 5.5 和 XMLWorker 将阿拉伯文本从 HTML 转换为 PDF

javascript - 如何用空格替换下划线?

html - CSS 背景内部阴影未填充完整背景