admin管理员组

文章数量:1414614

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

});

What I'm trying to do here is bind a different click event for the every 3rd <td> of each row and every other <td> different click function. 1st function works but how can I bind event for the <td>'s which are not the 3rd ones?

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

});

What I'm trying to do here is bind a different click event for the every 3rd <td> of each row and every other <td> different click function. 1st function works but how can I bind event for the <td>'s which are not the 3rd ones?

Share Improve this question asked Jan 7, 2013 at 5:03 TechieTechie 45.1k44 gold badges164 silver badges247 bronze badges 2
  • Try using on('click') instead of click. – Rutwick Gangurde Commented Jan 7, 2013 at 5:06
  • click function calls on(click). It's not the issue. Please read the question – Techie Commented Jan 7, 2013 at 5:07
Add a ment  | 

4 Answers 4

Reset to default 2

Pass the selector to the not method.

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

http://jsfiddle/z9HUB/

try this out live fiddle

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

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

Try $("td:not(:eq(2)")

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

You could do something like this:

$(document).ready(function() {

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

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

});

本文标签: javascriptBind different events with jQuery eq() and not()Stack Overflow