Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问表数据<;td>;逐行<;tr>;个别地_Javascript_Jquery_Html_Dom_Dynamic - Fatal编程技术网

Javascript 访问表数据<;td>;逐行<;tr>;个别地

Javascript 访问表数据<;td>;逐行<;tr>;个别地,javascript,jquery,html,dom,dynamic,Javascript,Jquery,Html,Dom,Dynamic,我需要对DOM进行更改。 我需要用paragraphs或类似的东西来呈现表格内容。为此,我需要获得表中每行的数据。我该怎么做 HTML示例: <table style="width:300px"> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr&g

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

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>
可以使用来遍历行,然后遍历单元格

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

现在,
groupedvales
包含的元素将根据您的期望进行分组

试试这个

$("table tr").each(function (i, val) {
   var t=$(this).map(function (i, val) {
    return $(this).text();
   }).get();
   alert(t);
});

$("table tr").each(function(){
  text = "";
  $(this).find('td').each(function(){
     text += $(this).text() + " ";      
  });
    alert(text);
});
$('table tr')。每个(函数(){
$('body').append($(this.text()+“
”); 警报($(this.text()); });
迭代每个
.text()属性…

为表提供一个ID(例如:mytable),然后执行如下操作:

$("#mytable").find("tr").each(function () {
  var rowtext = "";
  $(this).find("td").each(function () {
    rowtext += $(this).text() + " ";
  });
});
为什么是身份证?ID选择器比元素选择器快,确保代码只针对所需的表运行,如果希望在多个表上运行相同的代码,则类选择器将是理想的选择


为什么不是美元(“#mytable tr”)?jquery从右向左读取选择器,这意味着jquery将首先查找文档中的每个“tr”元素,然后根据它们是否有id为“mytable”的父元素进行筛选。像这样分离选择器:$(“#mytable”).find(“tr”)确保jquery只选择id为“mytable”的元素中的“tr”元素。

这是另一种解决方案。你可以试试

$('table tr').each(function(i,e){
    temp=[];
    $(this).find('td').each(function(){
        temp.push($(this).text());
    });
    resultBlock.append('"' + temp.join(' ') + '"');

    if($(this).index()!=($('table tr').length-1)) resultBlock.append(' and ');
});

试试看

$("table tr").each(function(){
  text = "";
  $(this).find('td').each(function(){
     text += $(this).text() + " ";      
  });
    alert(text);
});
   FLAG = true;
    text2 = "";
    $("table tr").each(function(){  
       text1="";       
       $(this).find('td').each(function(){
         text1 += $(this).text() + " ";      
         });
        if(FLAG)
        {
           text2=text1+" "+"and"+"  "
           FLAG=false;
        }
        else
        {
            text2+=text1
        }  
    });
    alert(text2);

是否有任何我可以添加的标题,以及像这样的内容。因为在第一个标签里面,标签就在那里。需要访问和。只有一次,因为它只在第一个标记内。您可能需要更改诸如,$(this).find('td,th').each(function(){…在演示中看到这一点,非常感谢。你太好了,Hey先生,你知道如何将表标题添加到变量或其他东西中,并在每个标记中使用它吗。例如,在上一个演示中,你发送给我的,我想得到输出,如-column1 Jill,Column2 Smith,column350。另一行也是同样的方式column1 Eve,Column2 Jackson,collumn3 94在浏览每个标签后,需要检查是否存在列名称(标题),然后将这些列名称(标题)保存到数组或其他东西中,因为我们不知道确切的列数。然后在浏览每个标签时,可以添加该列名称信息。谢谢。希望您能帮助我,先生
   FLAG = true;
    text2 = "";
    $("table tr").each(function(){  
       text1="";       
       $(this).find('td').each(function(){
         text1 += $(this).text() + " ";      
         });
        if(FLAG)
        {
           text2=text1+" "+"and"+"  "
           FLAG=false;
        }
        else
        {
            text2+=text1
        }  
    });
    alert(text2);