使用jQuery eq()绑定不同的事件而不是()

时间:2013-01-07 05:03:19

标签: javascript jquery html

HTML

<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
<td>row 1, cell 3</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
<td>row 2, cell 3</td>
</tr>
</table>

JS

$(document).ready(function() {

  $("tr").find("td").eq(2).click(function(){
      alert('hi');
  });

  $("tr").find("td").not().eq(2).click(function(){
      alert('hi2');
  });

});

我在这里尝试做的是为每行的每个第3 <td>和每个其他<td>不同的点击功能绑定不同的点击事件。第一个函数可以工作但是如何绑定<td>的事件不是第三个?

4 个答案:

答案 0 :(得分:2)

将选择器传递给not方法。

$("tr").find("td").not(':eq(2)').click(function(){
     alert('hi2');
});

http://jsfiddle.net/z9HUB/

答案 1 :(得分:1)

现场试试fiddle

    $(document).ready(function() {
      $("tr").find("td:eq(2)").click(function(){
        alert('hi2');
      });

      $("tr").find("td:not(:eq(2))").click(function(){
           alert('hi');
      });
    });

答案 2 :(得分:0)

尝试$("td:not(:eq(2)")

$("tr").find("td:not(:eq(2)").click(function(){
    alert('hi2');
});

答案 3 :(得分:0)

你可以这样做:

$(document).ready(function() {

  $("tr").find("td:eq(2)").click(function(){
      alert('hi');
  });

  $("tr").find("td:lt(2)").click(function(){
      alert('hi2');
  });

});