如何阻止对动态添加到标题的子元素的点击进行排序的DataTable?

这个问题听起来已经回答了。

Been looking for answers and I keep getting back to event.stopPropagation() answer. But why does it doesn't work on this fiddle? Am I missing something?

$(document).ready(function(){
  $('#test-table').DataTable()

  $('#test-table thead tr th').each(function(i, e) {
    $(this).append('<button id="test-button" style="margin-left: 5px; margin-right: 5px;">Test Button</button>')
  })

  $('body').on('click', '#test-button', function(e) {
    alert('test')
    e.stopPropagation()
  })

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" rel="stylesheet"/>

<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>


<table id="test-table" class="table table-striped table-bordered">
  <thead>
    <th>Name</th>
    <th>Description</th>
  </thead>
  <tbody>
    <tr>
      <td>a</td>
      <td>a</td>
    </tr>
    <tr>
      <td>b</td>
      <td>b</td>
    </tr>
  </tbody>
</table>
评论