【发布时间】:2019-09-11 20:31:46
【问题描述】:
我无法提取“LINE_NAME”的“href”值(预期值为“www.link.com”)。 它是一个表格的内容,它总是只有标题 + 1 行,但列顺序和编号可以不同。 “LINE_NAME”列始终以准确的格式存在
此调用返回“未定义”:
var url = $('.a-IRR-table tbody').children().eq(2).find('td[headers="LINE_NAME"] a').attr('href');
console.log(url);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table summary="Search Results" class="a-IRR-table" id="79" style="margin-top: -45px;">
<tbody>
<tr>
<th class="a-IRR-header"><a class="a-IRR-headerLink" data-column="625" role="presentation">Av</a>
<div class="t-fht-line"></div>
</th>
<th class="a-IRR-header"><a class="a-IRR-headerLink" data-column="437" role="presentation">CS</a>
<div class="t-fht-line"></div>
</th>
<th class="a-IRR-header"><a class="a-IRR-headerLink" data-column="167" role="presentation">LINE_NAME</a>
<div class="t-fht-line"></div>
</th>
<tr>
<td class=" u-tC" headers="AVAILABLE" aria-labelledby="AVAILABLE">
<img src="...png" alt="Av_ICON" title="Available" style="width:16px; padding-top:1px;">
</td>
<td class=" u-tL" headers="STATUS" aria-labelledby="STATUS">online</td>
<td class=" u-tL" headers="LINE_NAME" aria-labelledby="LINE_NAME">
<a href="www.link.com">url_link</a>
</td>
</tr>
</tbody>
</table>
【问题讨论】:
-
给
.eq()的参数是从零开始的,所以我猜你想要eq(1)而不是eq(2)。 -
只删除 .children() 和 .eq 一起: $('.a-IRR-table tbody').find('td[headers="LINE_NAME"] a').attr('参考文献');或 $('.a-IRR-table td[headers="LINE_NAME a').attr('href');
-
并删除 find....
$('.a-IRR-table tbody td[headers="LINE_NAME"] a') -
并简化为..
$('.a-IRR-table td[headers="LINE_NAME"] a').attr('href') -
感谢大家这么好的照明反应。不知道我怎么会错过编号,愚蠢的错误:)感谢简化和清洁的电话!
标签: javascript jquery