我有这个代码。它返回所有 td 代码,包括 td/td 标签。我希望它只返回 td 的内容/html
<td>Hey</td>
应该给我吧
Hey
jQuery("#ReportTable", html).each(function (index, tr) {
arr[index] = jQuery("tbody tr", tr).map(function (index, td) {
return jQuery(td).html();
});
});
jQuery 代码为我提供了一个如下所示的数组:
arr[0] = {"<td>1</td>", "<td>Hey</td>", "<td>Some data</td>" }
arr[1] = {"<td>2</td>", "<td>There</td>", "<td>Some other data</td>" }
从 html 看起来像这样:
<table id="ReportTable"><tr><td>1</td><td>Hey</td><td>Some data</td></tr><tr><td>2</td><td>There</td><td>Some other data</td></tr></table>
所以数组很好,除了我只需要 td 中的 html/文本。
最佳答案
您需要在选择器中再向下一层,如下所示:
jQuery(".ReportTable", html).each(function (index, tr) {
arr[index] = jQuery("tbody tr td", tr).map(function (index, td) { return jQuery(td).html(); });
});
或者,只需使用 .text()
相反或.html()
,像这样:
jQuery(".ReportTable", html).each(function (index, tr) {
arr[index] = jQuery("tbody tr", tr).map(function (index, td) { return jQuery(td).text(); });
});
(我有 ".ReportTable"
而不是 "#ReportTable"
,因为在问题 ID 上注明的注释必须是唯一的...因此您应该使用 class="ReportTable"
如果有多个)
关于javascript - jQuery:仅获取 <td> 的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2929082/