最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Children selector question - Stack Overflow

programmeradmin3浏览0评论

I have the following code:

$("#Table1 tbody").children().each(function(e){
$(this).bind('click', 
            function(){
                // Do something here
            }, 
            false) 
});

The Table1 html table has 2 columns; one for Names and one for a <button> element.

When I click on a table row, it works fine. When I click on the button, the button code fires; however, so does the row code.

How can I filter the selector so the button doesn't trigger the parent element's click event?

I have the following code:

$("#Table1 tbody").children().each(function(e){
$(this).bind('click', 
            function(){
                // Do something here
            }, 
            false) 
});

The Table1 html table has 2 columns; one for Names and one for a <button> element.

When I click on a table row, it works fine. When I click on the button, the button code fires; however, so does the row code.

How can I filter the selector so the button doesn't trigger the parent element's click event?

Share Improve this question asked Apr 14, 2009 at 0:14 JamesEggersJamesEggers 12.9k14 gold badges65 silver badges89 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

This is what you want.

It's stopPropogation that will stop the parents.

<table>
  <tr>
    <td>The TD: <input type="button" id="anotherThing" value="dothis"></td>
  </tr>
</table>

<div id="results">
  Results:
</div>

<script>

  $(function() {
    $('#anotherThing').click(function(event) {
       $('#results').append('button clicked<br>');
       event.stopPropagation();       
    });
    $('td').click(function() {
       $('#results').append('td clicked<br>');

    });
  });

</script>

Here's a link to an example of it working as well:

http://jsbin./uyuwi

You can tinker with it at: http://jsbin./uyuwi/edit

You could also do something like this:

$('#Table1 tr').bind('click', function(ev) {
  return rowClick($(this), ev);
}); //Bind the tr click
$('#Table1 input').bind('click', function(ev) {
  return buttonClick($(this), ev);
}) //Bind the button clicks

function rowClick(item, ev) {
  alert(item.attr('id'));
  return true;
}

function buttonClick(item, ev) {
  alert(item.attr('id'));
  ev.stopPropagation();

  return true;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="Table1">
  <tbody>
    <tr id="tr1">
      <td>
        The TD: <input type="button" id="button1" value="dothis" />
      </td>
    </tr>
    <tr id="tr2">
      <td>
        The TD: <input type="button" id="Button2" value="dothis" />
      </td>
    </tr>
  </tbody>
</table>

Is it possible to remove the button code and just run the row code, therefore kind of using event bubbling.

Another couple options:

  • can you add a class to the TD that has the button in it and in the selector, do '[class!="className"]'
  • maybe try event.preventDefault(). You can see it being used here. this way you can prevent the default action that is triggered when the button is clicked, although I'm not sure if it will pletely prevent the bubbling.
发布评论

评论列表(0)

  1. 暂无评论