gpt4 book ai didi

jquery - 使用 jquery 从数组创建表头

转载 作者:行者123 更新时间:2023-12-01 07:09:03 25 4
gpt4 key购买 nike

这是我的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

最佳答案

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

// 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>');
}

这是更新的 fiddle :http://jsfiddle.net/99f6ns5o/7/

关于jquery - 使用 jquery 从数组创建表头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32387087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com