如何在单击行时获取html表中第一列的值

4 javascript jquery

我有一个简单的HTML表.我想获得我点击的行的第一列的值.如果我点击第一行的任何地方它应该获得第一行的第一列的值,如果我点击第二行任何地方,它应该给第二列的第一列的值..这是我的表:

<table style="border:1px solid black" id="tab">
    <tr>
        <td style="border-right:1px solid black">Sachin</td>
        <td>Yuvraj</td> 
    </tr>
    <tr>
        <td style="border-right:1px solid black">Virat</td>
        <td>Gautam</td>              
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

coo*_*guy 6

    $(document).ready(function(){

    //use this if you have jquery version 1.7+
      $('table#tab tr').on('click',function(){
          alert($(this).find('td:first').text());

    });
    //normal way
  $('table#tab tr').click(function(){
          alert($(this).find('td:first').text());

    });
//delegation

$('body').delegate('table#tab tr','click',function(){
          alert($(this).find('td:first').text());

    });

//use any of these three methods

    });
Run Code Online (Sandbox Code Playgroud)