javascript - jQuery:仅获取 <td> 的 html

标签 javascript jquery

我有这个代码。它返回所有 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/

相关文章:

php - jquery ajax更新mysql表

javascript - 无法使用 jquery 选择特定元素

javascript - 重置 AJAX 表单

javascript - 使用 javascript/jquery 打印文本文件的内容

jquery - Flexslider 未显示在 IndexPage 上

javascript - 如果我在回调中不使用 `this` 关键字,是否应该使用可选的上下文参数?

javascript - 使用 VBScript 的 SOAP POST 请求

javascript - 按键排序 JavaScript 对象

javascript - 如何在javascript中添加一行

单个文件中的 Javascript 函数会导致冲突