最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set the id for a set of <tr>s in a table - Stack Overflow

programmeradmin1浏览0评论

I'm having a table with tr generating in a loop

<table>
    <div id="trresults">
    {FOR LOOP}
         <tr>
             <td>sample text</td>
             <td>sample text</td>
             <td>sample text</td>
         </tr>
         <tr>
             <td>sample text</td>
             <td>sample text</td>
             <td>sample text</td>
         </tr>
    {END FOR LOOP}
     </div>
</table>

I want to group the tr's and set a id for that. So that I used div for that, but div will not work as in table all the items should be inside td's. So how can I solve this problem?

I'm having a table with tr generating in a loop

<table>
    <div id="trresults">
    {FOR LOOP}
         <tr>
             <td>sample text</td>
             <td>sample text</td>
             <td>sample text</td>
         </tr>
         <tr>
             <td>sample text</td>
             <td>sample text</td>
             <td>sample text</td>
         </tr>
    {END FOR LOOP}
     </div>
</table>

I want to group the tr's and set a id for that. So that I used div for that, but div will not work as in table all the items should be inside td's. So how can I solve this problem?

Share Improve this question edited May 25, 2019 at 13:56 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jan 11, 2011 at 9:52 RajasekarRajasekar 19k35 gold badges109 silver badges140 bronze badges 6
  • 1 depending on what your goal is, maybe you can give all the tr's inside the loop the same class? – leeny Commented Jan 11, 2011 at 9:56
  • I want id to be set not classes – Rajasekar Commented Jan 11, 2011 at 9:56
  • Is there a reason you can't use multiple tables, and set the ID on each table? – Sam Dufel Commented Jan 11, 2011 at 9:59
  • what code generates all the tr's? can the same code assign a unique id to each tr? please give us some details on the {FOR LOOP} part. – KBoek Commented Jan 11, 2011 at 10:00
  • Hi Rajasekar, What is your reasoning for wanting an ID rather than class? – Alex KeySmith Commented Jan 11, 2011 at 10:04
 |  Show 1 more ment

1 Answer 1

Reset to default 6

You can use a TBODY element to group your rows

<table>
    <tbody id="trresults1">
      {FOR LOOP}
         <tr><td>for loop message</td></tr>
      {END FOR LOOP}
     </tbody>
    <tbody id="trresults2">
      {FOR LOOP}
         <tr><td>for loop message</td></tr>
      {END FOR LOOP}
     </tbody>
</table>

Reference: Mozilla HTML Reference

  • <tbody>
  • <table>
发布评论

评论列表(0)

  1. 暂无评论