I have a html table generated by BIRT as follow:
<table id="myTableID">
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</table>
I want to write a JavaScript code that reads this table and re-writes it in the following form: getting the first row of the table in a <thead>
tag, and the rest of the table in the <tbody>
tag:
<table id="myTableID">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</tbody>
</table>
I have a basic knowledge about the JavaScript, but I have no clue how to proceed with such case. Please any help?
I have a html table generated by BIRT as follow:
<table id="myTableID">
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</table>
I want to write a JavaScript code that reads this table and re-writes it in the following form: getting the first row of the table in a <thead>
tag, and the rest of the table in the <tbody>
tag:
<table id="myTableID">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</tbody>
</table>
I have a basic knowledge about the JavaScript, but I have no clue how to proceed with such case. Please any help?
Share Improve this question edited Nov 26, 2015 at 20:35 Thilina Sampath 3,7737 gold badges42 silver badges66 bronze badges asked Oct 1, 2015 at 14:00 DhohaDhoha 3693 gold badges6 silver badges17 bronze badges 3-
$('#myTableID tr:first').wrap('<thead />')
– Tushar Commented Oct 1, 2015 at 14:02 -
Basically
th
is forthead
andtd
is fortbody
. Do you needth
for tbody as well? – Deepak Biswal Commented Oct 1, 2015 at 14:14 -
If
td
is OK then please let me know I'll post my answer.. – Deepak Biswal Commented Oct 1, 2015 at 14:17
1 Answer
Reset to default 7- Use prependTo() to insert the
thead
element - Use append() to insert the first
tr
inside thethead
$('<thead></thead>').prependTo('#myTableID').append($('#myTableID tr:first'));
console.log($('#myTableID')[0].outerHTML);
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<table id="myTableID">
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</table>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745431229a4627399.html
评论列表(0条)