使用数组中的jquery创建表头

时间:2015-09-03 23:42:22

标签: jquery html html-table

这是我的fiddle

我想要做的是使用jquery创建它:

   <thead>
    <tr>
        <th>DeviceName</th><th>counter1</th><th>counter2</th><th>counter3</th><th>counter4</th> 
    </tr>
  </thead> 

并将其附加到此

       <table id="counterTableDomId3" class="display">

       <table>    
但是,我害怕我相当有限。这就是我到目前为止所提出的。

//this is the array 
arr1=["DeviceName", "counter1", "counter2", "counter3", "counter4"];

        $('#counterTableDomId3').append($div3)
        //want to then append to <tr> and iterate through the array 

1 个答案:

答案 0 :(得分:4)

您可以循环遍历数组,为数组中的每个元素创建一个:

// First create your thead section
$('#counterTableDomId3').append('<thead><tr></tr></thead>');

// Then create your head elements
$thead = $('#counterTableDomId3 > thead > tr:first');
for (var i = 0, len = arr1.length; i < len; i++) {
    $thead.append('<th>'+arr1[i]+'</th>');
}

这是一个更新的小提琴:http://jsfiddle.net/99f6ns5o/7/