2011-12-12 147 views
0

我想要顯示錶格(存在於定位點內)的標題列文本並將警報事件綁定到定位點。我已經試過的東西,但它不工作爲定位點添加點擊事件

<table style="width:20%;border:1;border-style:solid" > 
     <tr> 
      <td> 
       <a href="#">Header 1</a> </td> 
      <td> 
       <a href="#">Header 2</a></td> 
      <td> 
       <a href="#">Header 3</a></td> 
     </tr> 
     <tr> 
      <td> 
       R1C1</td> 
      <td> 
       R1C2</td> 
      <td> 
       R1C3</td> 
     </tr> 
     <tr> 
      <td> 
       R2C1</td> 
      <td> 
       R2C2</td> 
      <td> 
       R2C3</td> 
     </tr> 
    </table> 

請找到使用jQuery的

<script type="text/javascript"> 

     $(document).ready(function() { 
      //should display Header 1, Header 2, Header 3 
      $("table tr td a").each(alert($(this).text())); 
      //Add click event for anchor 
      $("table tr td a").click(function (e) { alert(); }) 
     }); 

    </script> 

回答

1
$(document).ready(function() { 
     //should display Header 1, Heder 2, Header 3 
     $("table tr td a").each(function() {// need a function here 
      alert($(this).text()) 
     }); 
     //Add click event for anchor 
     $("table tr td a").click(function (e) { alert($(this).text()); return false;}) 
    });