添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Can i copy the attribute of the thead tr td? to the tbody tr td? so it will also become

<table>
 <thead>
   <td data-date="2017-10-08"></td>
   <td data-date="2017-10-09"></td>
   <td data-date="2017-10-10"></td>
   <td data-date="2017-10-11"></td>
   <td data-date="2017-10-12"></td>
 </thead>
 <tbody>
   <td data-date="2017-10-08"></td>
   <td data-date="2017-10-09"></td>
   <td data-date="2017-10-10"></td>
   <td data-date="2017-10-11"></td>
   <td data-date="2017-10-12"></td>
 </tbody>

Note that I cant manully put the attribute coz this is coming from a plugin

$(this).data('date', xyz[i]).html(xyz[i]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border='1' celpadding="2">
 <thead>
   <td data-date="2017-10-08"></td>
   <td data-date="2017-10-09"></td>
   <td data-date="2017-10-10"></td>
   <td data-date="2017-10-11"></td>
   <td data-date="2017-10-12"></td>
 </thead>
 <tbody>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
 </tbody>
 </table>
theadTds.each((index, td) => { const tbodyTd = tbodyTds.eq(index); // Get current tbody td tbodyTd.data('date', $(td).data('date')); // Set the `data-date` to the `thead td` `data-date` console.log(tbodyTd.data('date'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
 <thead>
   <td data-date="2017-10-08"></td>
   <td data-date="2017-10-09"></td>
   <td data-date="2017-10-10"></td>
   <td data-date="2017-10-11"></td>
   <td data-date="2017-10-12"></td>
 </thead>
 <tbody>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
 </tbody>
Ah, you about arrow function. Just change it into the function. But I think every front end code must be transpiled before the production – Suren Srapyan Oct 10, 2017 at 12:09

No need for any interim variables etc. Just iterate over the columns, this does ALL rows in body, if you just want first one you can do that also.

$('thead').find('tr').find('td').each(function(index){ 
   $('tbody').find('tr').find('td').eq(index).data('date',$(this).data('date')):

Only first row in body

$('thead').find('tr').find('td').each(function(index){ 
   $('tbody').find('tr').eq(0).find('td').eq(index).data('date',$(this).data('date')):

Alternate syntax if that is more clear to you

$('thead').find('tr').find('td').each(function(index,element){ 
   $('tbody').find('tr').eq(0).find('td').eq(index).data('date',$(element).data('date')):
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.