1
<table>
    <tr>
        <th>
            Title
        </th>
        <th>
            Author
        </th>
    <tr>
</table>

这是基本表

$("th").click(function(){
    $(this).method();
});

我想要做的就是在点击标签时调用一个方法,但我想知道点击了哪个标签。因此,如果单击标题,我希望它返回 0,因为它是第一个元素,如果单击作者,则返回 1,因为它是第二个元素。

我尝试将 data-val= 放在 th 标记上,但这没有验证,所以我删除了它。

4

1 回答 1

7

你需要的正是你上面所拥有的,但是使用$(this).index(). 这是一个工作小提琴......

http://jsfiddle.net/WNVbn/

于 2012-12-20T13:12:52.533 回答