jQuery add and add

前端 未结 3 633
既然无缘
既然无缘 2021-01-05 10:03

How do I add and this using jQuery?

the problem is my table has 1 or 2 th rows?

$(\'#myTable tr         


        
相关标签:
3条回答
  • 2021-01-05 10:32
    function createTable(data) {
        var str = "";
        str += '<table><thead>';
        str += '<tr><td>Pos</td><td>Ref</td></tr></thead><tbody>';
        for (var item in data.recentList) {
            str += '<tr>';
            for (idata in data.recentList[item]) {
                str += '<td>' + data.recentList[item][idata] + '</td>';
            }
            str += '</tr>';
        }
        str += '</tbody></table>';
        $('body').append(str);
    }
    

    Working version that creates a table from an array

    0 讨论(0)
  • 2021-01-05 10:33

    What you need to do is remove the rows and append them to a thead element

    var myTable = jQuery("#myTable");
    var thead = myTable.find("thead");
    var thRows =  myTable.find("tr:has(th)");
    
    if (thead.length===0){  //if there is no thead element, add one.
        thead = jQuery("<thead></thead>").appendTo(myTable);    
    }
    
    var copy = thRows.clone(true).appendTo("thead");
    thRows.remove();
    

    jsFiddle exmaple ​

    0 讨论(0)
  • 2021-01-05 10:43

    use wrapAll instead of wrap

    $('#myTable tr:has(th)').wrapAll('<thead></thead>');​
    $("#myTable thead").prependTo("#myTable")
    
    0 讨论(0)
提交回复
热议问题