简体   繁体   English

如何在动态点击tr列中获取tr的第二个td

[英]how to get second td for tr in dynamic click tr column

how to get second td text value.when i click any tr column must show to second td text value 如何获得第二个td文本值。当我单击任何tr列时,必须显示第二个td文本值

 td:hover{ background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <table border="1px"> <thead> <tr> <th>row1</th><th>row2</th><th>row3</th><th>row4</th><th>row5</th> </tr> </thead> <tbody> <tr data-class="weeks"> <td class="cls">1</td><td class="cls">2</td><td>3</td><td>4</td><td>5</td> </tr> <tr data-class="weeks"> <td>6</td><td>7</td><td>8</td><td>9</td><td>10</td> </tr> </tbody> </table> 

 $('td').click(function(){//add click event on each td console.log($(this).closest('tr').find('td:nth-child(2)').text());//log the result of getting the second td text }) 
 td:hover{ background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <table border="1px"> <thead> <tr> <th>row1</th><th>row2</th><th>row3</th><th>row4</th><th>row5</th> </tr> </thead> <tbody> <tr data-class="weeks"> <td class="cls">1</td><td class="cls">2</td><td>3</td><td>4</td><td>5</td> </tr> <tr data-class="weeks"> <td>6</td><td>7</td><td>8</td><td>9</td><td>10</td> </tr> </tbody> </table> 

$(this).closest('tr').find('td:nth-child(2)').text()
  1. Using .closest('tr') get the row of the clicked td 使用.closest('tr')获得所单击td的行
  2. Using .find('td:nth-child(2)') get the second td of the clicked row 使用.find('td:nth-child(2)')获得被点击行的第二个td
  3. Using .text() get the text of the second td of the clicked row 使用.text()获取所单击行的第二个td的文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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