【问题标题】:how to extract attribute value using javascript如何使用javascript提取属性值
【发布时间】: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


【解决方案1】:

您的问题是 .eq() 函数来自 jQuery is 0 based,因此要获得第二个元素,您必须使用 .eq(1)

var url = $('.a-IRR-table tbody').children().eq(1).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">
	<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>

【讨论】:

    【解决方案2】:
    var url = $('.a-IRR-table').find('td[headers="LINE_NAME"] a').attr('href');
    
    console.log('url = ' + url);
    

    应该有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 2019-04-12
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多