繁体   English   中英

jQuery表格的每一行

[英]jQuery each row of a table

我有一个包含3行的表,并且我试图遍历该表并获取该表各行中链接的title属性,将其分配给变量并将其放入函数中。 以下是我正在使用的代码:

$('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演示

问题在于此$(this+".location a")语句。

尝试

postcode = $(this).find("td.location a").attr('title');
console.log(postcode);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM