0

我的桌子是这样的:

<table>
    <tr>
       <td>
           <img src="" />
           <span>blah</span>
       </td>
    </tr>
    ...

所以在每一行中,第一列包含一个图像。我需要在每个图像上注册事件。那么我该如何选择呢?

我试过这个:

td:eq(0):nth-child(0)

但它不起作用。

4

8 回答 8

2

something like this selector:

td:first-child > img

should work i guess...

于 2013-06-18T08:26:06.207 回答
2

对于nth-child选择器,索引是基于 1 的。我认为这样的事情对你有用:

$("td:nth-child(1) > img")

甚至更简单:

$("td:first-child > img")
于 2013-06-18T08:28:18.067 回答
1

我建议:

$('table tr td:first-child img').on('event', function (){
    // handle the event here
});
于 2013-06-18T08:27:21.137 回答
0

I think http://forum.jquery.com/topic/jquery-how-to-select-all-first-tds-inside-all-tr-in-a-table contains several solutions:

$('tr td:first-child')

$('tr').find('td:eq(0)')

$("tr td:nth-child(1)")

于 2013-06-18T08:25:59.460 回答
0
$('table > tr > td:first-child > img').bind('click', function() {
   // your code
});

I would give the table a unique id for more precision.

$('#table_id > tr > td:first-child > img').bind('click', function() {
   // your code
});

Replace 'click' in the code above with wathever event you need to check.

于 2013-06-18T08:26:56.603 回答
0

检查这个

$(function(){
$("table tr td:first-child").on("click","img",function(){
    console.log("hey");
});
});
于 2013-06-18T08:48:17.983 回答
0

您可以选择表格的第一列图像,如下所示:

$('tr').find('td:eq(0)')
于 2013-06-18T08:28:56.737 回答
0

看到您想将事件附加到图像,而不是迭代到每个图像,您可以像这样绑定事件处理程序:

$("table").on("eventname", "td:first-child img", function(i){
 //your code
});

演示:http: //jsfiddle.net/hungerpain/PYFhw/

于 2013-06-18T08:24:35.940 回答