javascript - 如何使用javascript提取属性值

标签 javascript jquery

我在提取“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>

最佳答案

你的问题是 jQuery is 0 based 中的 .eq() 函数,因此要获取第二个元素,您必须使用 .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>

关于javascript - 如何使用javascript提取属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57889031/

相关文章:

jquery 更改父级的 css

javascript - 无法让我的页面正常工作

javascript - 资源的扩展名与其实际内容不一致?

javascript - 使用 ember 数据如何单独异步获取子项?

javascript - Xrm.Utility.openEntityForm 设置查找字段

jquery - 是否可以在 Visual Studio 2008 中使用 Intellisense 的内容交付网络?

javascript - 使用 .html() 选择后的 jQuery 编辑 innerHTML

javascript - 内联 CKEditor 关闭后获取修改内容

javascript - 有效加载重复图像

php - 使用 functions.php 将 Bootstrap 排队到 Wordpress