简体   繁体   中英

append table row after every fifth row using javascript/jQuery

I need to append a table row count after every 5th row in a table. How can I do this without using server side programming? jQuery or regular javascript is fine!

I would like this table:

   <table>
      <tr><td>1</td></tr>
      <tr><td>2</td></tr>
      <tr><td>3</td></tr>
      <tr><td>4</td></tr>
      <tr><td>5</td></tr>
      <tr><td>6</td></tr>
      <tr><td>7</td></tr>
      <tr><td>8</td></tr>
      <tr><td>9</td></tr>
      <tr><td>10</td></tr>
      <tr><td>11</td></tr>
   </table>

To become this:

   <table>
      <tr><td>1</td></tr>
      <tr><td>2</td></tr>
      <tr><td>3</td></tr>
      <tr><td>4</td></tr>
      <tr><td>5</td></tr>
      <tr><td>--5th--</td></tr>
      <tr><td>6</td></tr>
      <tr><td>7</td></tr>
      <tr><td>8</td></tr>
      <tr><td>9</td></tr>
      <tr><td>10</td></tr>
      <tr><td>--10th--</td></tr>
      <tr><td>11</td></tr>
   </table>

here you are

$("table tr:nth-child(5n)").after("<tr><td> new row </td></tr>")

update

or even beter

$("table tr:nth-child(5n)").each(function() {
    $(this).after("<tr><td>--"+$("td", this).text()+"th--</td></tr>")
});

Try -

$("table tr:nth-child(5n)").after("<tr><td>--added--</td></tr>")

Demo - http://jsfiddle.net/SZpuh/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM