2011-02-18 30 views
1

如何在單擊單元格時使用Jquery獲取列名稱?Jquery,單擊單元格時獲取列名

+6

來吧,你可以做得比這更好。請提供一些細節,代碼示例和更多上下文。這不是一個猜謎遊戲。 – 2011-02-18 08:34:35

回答

0

HI,

請看看this

我不知道這是否適合您的答案。這只是我做的一個樣本。如果我錯了,請糾正我。

5

您可以通過獲取單元格的索引,然後從具有相同索引的標題中獲取文本。

我已上載這裏演示:

http://jsfiddle.net/Sohnee/DNxTz/23/

jQuery的是這樣的:

$("td").click(function(){ 
    var $This = $(this); 
    var col = $This.parent().children().index($(this)); 
    var title = $This.closest("table").find("th").eq(col).text(); 
    alert(title); 
}); 

,並依靠正確的表結構....

<table> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Address</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>Steve</td> 
      <td>UK Somewhere</td> 
     </tr> 
     <tr> 
      <td>Scott</td> 
      <td>USA Somewhere</td> 
     </tr> 
    </tbody> 
</table> 

注意:caption,tfoot等人,省略可選的東西都可以加入,sc ript仍然有效。

http://www.w3.org/TR/html401/struct/tables.html