如何限制应用于<身体>在主体中的jquery数据表上执行

how to limit a click event applied to the <body> from executing on a jquery datatable in the body

本文关键字:jquery 数据表 执行 主体 gt 应用于 何限制 lt 身体      更新时间:2023-09-26

我有动画数据表,当点击它们的超链接时,它们会从左侧滑入。当用户读取完可见数据表的内容后,我应用了以下代码,允许用户单击其他任何位置,将表停在一边并继续查看。我使用jQuery代码附加点击事件。。。

<script type="text/javascript" charset="utf-8">
    $(document).ready( function () {$('.dtable').dataTable( {"sDom": 'rt',"sScrollY":"200px", "bPaginate":false, "bFilter":false} );**$('body').click(function() {parkDataTables();});})
</script>

不幸的是,点击数据表本身会使它停下来。我不希望这种行为。也许有人知道如何阻止这个点击事件在数据表的表面上触发。。。

非常感谢

Dennis

您可以使用而不是使用body作为选择器

$('body').children().not('.dtable')

所以你会得到

<script type="text/javascript" charset="utf-8">
$(document).ready( 
  function () {
   $('.dtable').dataTable( {"sDom": 'rt',"sScrollY":"200px", "bPaginate":false, "bFilter":false} );
$('body').children().not('.dtable').click(function() {
  parkDataTables();
 });})
</script>

您应该使用

$('.dtable').click(function(){return false;});

问题是,当您单击该表时,事件会先进入表,然后传播到父级(最后传播到body,当parkDataTables被捕获时)。

您也可以使用stopPropagation(http://api.jquery.com/event.stopPropagation/),因为使用return false还可以停止表上的默认单击行为。

$('.dtable').click(function(event){
    event.stopPropagation();
});  

也许你可以查看此页面,看看最后的区别:http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/

Jquery通过每个父级向上传播函数。为了停止传播,必须"return false;"。

在您的情况下,您想尝试:

> $('body').click(function() { if $(this).hasClass('dtable'){return
> false;}) if $(this).hasClass('body'){park}

给你的身体一个"身体"类,让它可以选择。