0

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');
  });

});

ここで私がやろうとしているのは、<td>各行の3番目ごとと他のすべての<td>異なるクリック関数に対して異なるクリックイベントをバインドすることです。1番目の関数は機能しますが、3番目の関数ではないのイベントをバインドするにはどうすればよい<td>ですか?

4

4 に答える 4

2

notセレクターをメソッドに渡します。

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

http://jsfiddle.net/z9HUB/

于 2013-01-07T05:09:11.100 に答える
1

これをライブフィドルで試してください

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

      $("tr").find("td:not(:eq(2))").click(function(){
           alert('hi');
      });
    });
于 2013-01-07T05:20:43.803 に答える
0

次のようなことができます。

$(document).ready(function() {

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

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

});
于 2013-01-07T05:11:41.753 に答える
0

試す$("td:not(:eq(2)")

$("tr").find("td:not(:eq(2)").click(function(){
    alert('hi2');
});
于 2013-01-07T05:11:05.933 に答える