Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery选择器.not()不工作_Javascript_Jquery - Fatal编程技术网

Javascript jQuery选择器.not()不工作

Javascript jQuery选择器.not()不工作,javascript,jquery,Javascript,Jquery,我为函数构建了一个jQuery选择器,如下所示: $('html').not('.table-main tr[selected]').mousedown( function( e ) { 但不知何故,它根本不是过滤,我不太明白为什么。 即使我只是将“.table main”保留为选择器,我仍然会在单击表格时触发该函数。。。这有什么不对 使用document或“body”代替“html”没有帮助,因为document根本不会触发。not和“body”的结果是相同的 编辑: 只是试着用桌子

我为函数构建了一个jQuery选择器,如下所示:

    $('html').not('.table-main tr[selected]').mousedown( function( e ) {
但不知何故,它根本不是过滤,我不太明白为什么。 即使我只是将“.table main”保留为选择器,我仍然会在单击表格时触发该函数。。。这有什么不对

使用document或“body”代替“html”没有帮助,因为document根本不会触发。not和“body”的结果是相同的

编辑: 只是试着用桌子外面的另一个街区,效果不错。有没有可能我不能在桌子上用这个

Update2:根据请求,以下是表的呈现html代码:

    <table border="0" cellspacing="2px" cellpadding="5px" class="table-main">
      <tr id="tablefields">
        <th style="padding: 0;">
          <table cellpadding="0" cellspacing="0" border="0" align="center">
            <tr><th><div class="tr-head-get" id="tr-get-0">Name</div></th></tr>
            <tr><th><div class="th-header" id="th-header-0" style="top: 54px;">Name</div></th></tr>
          </table>
        </th>    
      <th style="padding: 0;"></th>
    </tr>
      <tr id='table_form_new_device' class='table_form_class'>
      <form accept-charset="UTF-8" action="/devices" class="new_device" data-remote="true" id="new_device" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="T2dSeZpxxvgJdTP+yoE7BrVODzqJ95gyVu9Wh/v7oP8=" /></div>
        <th class='tablefield'>
          <input id="device_devicename" name="device[devicename]" size="10" type="text" />
        </th>
        <th>
          <div class='submitbox' id='submitbox_new_device'>S</div>
          <script>
            $('#submitbox_new_device').click(function (event) {
              $('#new_device').submit();
            });
          </script>
        </th>
    </form></tr>
    </table>
    <div class="dropdown-button" style="margin: 0 2px;">Filter</div>
在tr类型的元素上使用[selected]没有意义。您将selected用于inputscheckbox,radio。您可以将类.selected添加到.main表中的tr元素中,然后使用:

编辑:与上面发布的某人一样,您希望使用选择嵌套在主体内的元素

$('body').find(':not(.selected)').mousedown(function(e){})
$'html'创建一个包含html元素的jQuery对象

.not'.table main tr[selected]'删除任何具有选定属性的tr元素。此处有一个错误,该属性不会出现在从属于table main类成员的元素派生的tr上。由于HTML元素不是TR元素,不能从任何元素派生,因此这不会删除任何元素,也不会产生任何效果

mousedown函数将事件处理程序绑定到HTML元素。除非有什么东西停止传播,否则任何点击最终都会冒泡到那里

我猜,因为您实际上并没有说您想要实现什么,所以您希望捕获未选中的TR元素上的点击。假设您切换到使用类使HTML有效

$('.table-main tr:not(.selected)').mousedown(…);


您确实非常希望为此使用委托事件处理,因为您不希望将事件处理程序附加到文档中的每个元素。您希望在文档级别截获冒泡事件,只需检查mousedown是否来自不是表中选定行的元素

此外,tr[selected]似乎不太可能工作。我认为您需要将所选类添加到所选行,然后使用tr.selected作为选择器

如果你做了这样的改变,我建议你做以下两种选择之一:

$(document).mousedown(function(e) {
    if (!$(e.target).is('.table-main tr.selected')) {
       // mouse down and it wasn't in a selected row of your table)
    }
})
您还可以让jQuery委派完成更多的工作:

$(document).on('mousedown', ':not(.table-main tr.selected)', function() {
   // mouse down and it wasn't in a selected row of your table)

});
在看到您的实际HTML后,如果我正确理解了问题,这应该可以工作。只要单击不在包含所选类的主表的行中,这将在单击发生时为您提供一个事件:

$(document).on('mousedown', function(e) {
   // mouse down and it wasn't in a selected row of your table)
    e.stopPropagation();
    if (!$(e.target).closest(".table-main tr.selected").length) {
        console.log("mousedown not in selected row of table-main")
    }
});​

这里有一个演示:

也可能是事件冒泡问题。阅读更多关于事件顺序的信息,以便尝试选择每个未选择的tr?请看下面的图片。你的语法看起来有点不对劲。不过,一些DOM示例可能会对您提供更多帮助。实际上,它应该是通过.data分配的布尔值,但我猜这是不正确的。syntaxI希望在mousedown上调用事件,而不是仅在这一行中调用。[selected]应该是通过.data分配给行的布尔值。为了简化事情,我只能坐在桌子上,但它不起作用。使用$'html:not.table main'仍在对.table main作出反应。如果我只使用.table main并单击单元格之间的间距,这实际上是可行的。如果我直接单击单元格,则不会。我是否也必须将表中的每个类分配给选择器?将表的实际HTML添加到您的问题中,我们可以提供更具体的帮助。您还需要更具体地说明您希望在该处理程序中处理和不希望处理哪些单击。这使用事件冒泡和事件委派来优雅地解决问题。另外,您尝试了两个选项中的哪一个?以较短的形式发布呈现的html,因为实际表格在该html中的长度为5英里,在该事件处理程序中,您希望和不希望单击哪个按钮?假设我希望除“table\u form\u class selected”之外的所有选项上的选择器触发该函数
$(document).on('mousedown', function(e) {
   // mouse down and it wasn't in a selected row of your table)
    e.stopPropagation();
    if (!$(e.target).closest(".table-main tr.selected").length) {
        console.log("mousedown not in selected row of table-main")
    }
});​