javascript - 逐行 <tr> 单独访问表数据 <td>

标签 javascript jquery html dom dynamic

我需要对 DOM 进行更改。 我需要使用 paragaraphs 或类似的东西来呈现表格内容。为此,我需要获取表格每一行的数据。我该怎么做?

HTML 示例:

<table style="width:300px">
    <tr>
        <td>Jill</td>
        <td>Smith</td> 
        <td>50</td>
    </tr>
    <tr>
        <td>Eve</td>
        <td>Jackson</td> 
        <td>94</td>
    </tr>
</table>

输出应该是这样的 - Jill Smith 50Eve Jackson 94

我写过这样的东西,但它遍历了所有 <td>网页中的标签。不按行。

 $("table").each(function() {
    $("tr").each(function(){
        $("td").each(function(){                
            label = $('td').text(); 
        });         
    alert(label);
 });

最佳答案

您可以使用 each()遍历行,然后遍历单元格

Live Demo

$("table tr").each(function(){
  text = "";
  $(this).find('td').each(function(){
     text += $(this).text() + " ";      
  });
    alert(text);
});

关于javascript - 逐行 <tr> 单独访问表数据 <td>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23907410/

相关文章:

javascript - 根据鼠标滚动切换选项卡

javascript - 在javascript函数中调用Jsp页面

javascript - 是否可以隐藏 AngularJS 生成的 HTML 属性?

javascript - 如何使用Jquery获取基于文本的特定超链接对象

javascript - 使用 JavaScript 数组在字符串中添加两个字母的百分比

jquery - 如何将过渡属性设置为 jquery 响应元素?

jquery - 如何在播出日期选择器日历中突出显示当前星期

javascript - 将 javascript 函数附加到 div

html - 旧学校边框图像 - css2 -

html - 缩小时页面主体不会延伸到显示底部