如何从click事件中排除子类?

pea*_*ove 0 jquery children

我想noclick从click函数中排除该类:

$( ".click" ).on( "click", ":not(.noclick *)", function() {
  console.log( $( this ).text() );
});
Run Code Online (Sandbox Code Playgroud)
.noclick{
background-color:pink;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
  <tr class="click">
    <th>Click</th>
    <th>Click</th>
    <th class="noclick">Nothing should happen here</th>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

Phi*_*ter 6

只需从中删除星号即可 ":not(.noclick *)"

这使得它.noclick的后代上的事件不会触发该函数,但不会阻止.noclick自身.

$( ".click" ).on( "click", ":not(.noclick)", function() {
  console.log( $( this ).text() );
});
Run Code Online (Sandbox Code Playgroud)
.noclick{
background-color:pink;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
  <tr class="click">
    <th>Click</th>
    <th>Click</th>
    <th class="noclick">Nothing should happen here</th>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)