Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 需要使用“for循环”从表中提取数据的帮助吗_Javascript_Jquery_Loops_For Loop_Extract - Fatal编程技术网

Javascript 需要使用“for循环”从表中提取数据的帮助吗

Javascript 需要使用“for循环”从表中提取数据的帮助吗,javascript,jquery,loops,for-loop,extract,Javascript,Jquery,Loops,For Loop,Extract,不知道有没有人能帮我 我创建了一个表,并使用Javascript提取所有数据并将其放入div中 $(function () { $('table').each(function () { var output = "", table = $(this), rowHead = table.find('tbody tr th'), rowSubject = table.find('thead tr th:

不知道有没有人能帮我

我创建了一个表,并使用Javascript提取所有数据并将其放入div中

$(function () {
    $('table').each(function () {
        var output = "",
            table = $(this),
            rowHead = table.find('tbody tr th'),
            rowSubject = table.find('thead tr th:not(:first-child)'),
            rowContent = table.find('tbody tr td'),
            copy = table.clone();

        output += '<div class="mobiled-table">';
        for (i = 0; i < rowHead.length; i++) {
            output += '<div class="head">' + $(rowHead[i]).html() + '</div>';
            for (j = 0; j < rowSubject.length; j++) {
                output += '<div class="subject">' + $(rowSubject[j]).html() + '</div>';
                output += '<div class="content">' + $(rowContent[i]).html() + '</div>';
            }
        }
        output += '</div>';
        $('table').append(output);
    });
});
除了.content类不能正常工作外,所有这些都非常有效。我认为我使用了错误的“for循环”,或者我需要创建另一个“for循环”。请看一下我的密码笔,你会发现我的问题

我希望有人能提供帮助。

因为rowContent包含单元格矩阵,但作为一维数组,您必须将I,j转换为rowContent的有效索引,即I*4+j:

应替换为:

rowContent[(i*4) + j]

简单地说,你可以这样做

var container=$("#container");
$("table tr:not(:first)").each(function() {
    var heading = $(this).find("th").html();
    container.append('<div class="subject">' + heading + '</div>');
    $(this).find("td").each(function(i) {
        container.append('<div class="subject">' + $("table th").eq($(this).index()).html() + '</div>');
        container.append('<div class="content">' + $(this).html() + '</div>');
    });
});

您尚未在css中创建.content类??我是布里尔。非常感谢,我会调查的。它看起来比我的代码干净多了。谢谢你抽出时间。非常感谢。
var container=$("#container");
$("table tr:not(:first)").each(function() {
    var heading = $(this).find("th").html();
    container.append('<div class="subject">' + heading + '</div>');
    $(this).find("td").each(function(i) {
        container.append('<div class="subject">' + $("table th").eq($(this).index()).html() + '</div>');
        container.append('<div class="content">' + $(this).html() + '</div>');
    });
});