Jquery, get columname when cell clicked
How can I get column name with Jque开发者_如何学编程ry when any cell is clicked?
You can do it by obtaining the index of the cell, and then getting the text from the header with the same index.
I have uploaded a demo here:
http://jsfiddle.net/Sohnee/DNxTz/23/
The jQuery looks like this:
$("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);
});
And relies on a proper table structure....
<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>
Note: caption, tfoot et al, omitted optional stuff can all be added and the script still works.
http://www.w3.org/TR/html401/struct/tables.html
HI,
Please have a look at this.
I don't know if this is proper answer for you or not. This is just a sample I have made. Please correct me if I am wrong.
精彩评论