gpt4 book ai didi

javascript - 如何通过单击从表格行中获取文本

转载 作者:行者123 更新时间:2023-11-29 20:51:09 26 4
gpt4 key购买 nike

我有表格,有时我需要从特定行获取表格文本。

例如这里是行:

<tr>
<td>
Tompson Road
<span style="display: none;">927</span>
</td>
<td>5</td>
</tr>

我需要获取此文本:汤普森路

我是这样做的:

    $("#<%= gvFeatList.ClientID %> tr").click(function () {
var text = $(this).context.innerText;
});

但文本包含:

汤普森路 927 号

虽然我只需要 - 汤普森路

如何更改此命令:$(this).context.innerText 以仅获取文本?

最佳答案

这也是您问题的可能解决方案!由于您只需要第一个 td 中的内容,您可以使用 find() 方法并单独拟合该特定元素。

$("#test tr").click(function() {
var text = $(this).find('td:first-child')[0].innerText;
console.log(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table id="test">
<tr>
<td>
Tompson Road
<span style="display: none;">927</span>
</td>
<td>5</td>
</tr>
</table>

关于javascript - 如何通过单击从表格行中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51809918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com