繁体   English   中英

jQuery .NOT()$(this)或$(this).parent()

[英]jQuery .NOT() $(this) or $(this).parent()

我有下面的代码,我试图从射击的停止,如果.hide()两次,目前使用的.not() 这两种情况是:

  • $(this).parent()。closest(“。HoverChild”)
  • $(this).children(“。HoverChild”)

编辑:因为它没有使用第二个$(this)所以它隐藏了子元素,然后再次显示它。

 $(".HoverContainer").on("hover click",function (e) { $('.HoverChild').not( $(this).parent().closest(".HoverChild"), $(this).children(".HoverChild") ).hide(); if ($(".HoverContainer").is(e.target)){e.stopPropagation();} $(this).children('.HoverChild').stop(true,true).slideToggle(100); }); $("body").on("hover click",function (e){ if (!$(".HoverContainer").is(e.target) && $(".HoverContainer").has(e.target).length === 0) { $(".HoverContainer").children('.HoverChild').hide(); }}); $(".HoverChild").on("hover click",function (e) { e.stopPropagation(); }); 
 html, body{ WIDTH: 100%; HEIGHT: 100%} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <span class="HoverContainer" style="Float:Left;"> <img src="" alt="View Categories" style="width:20px;height:20px;"> <div class="HoverChild" style="display: none;"> <ol class="top-nav" > <li class="HoverContainer" >Parent <ul class="HoverChild" style="display: none;"> <li><a href="javascript:void(0);">Sub 1</a></li> <li><a href="javascript:void(0);">Sub 2</a></li> </ul> </li> </ol> </div> </span> 

您的帮助将不胜感激。 非常感谢。

glenn2223

not()仅接受一个参数。 您可以通过三种选择来实现自己想要的。 第一个是将数组传递给not()函数,如下所示:

$('.HoverChild').not([
    $(this).parent().closest('.HoverChild'),
    $(this)
]).hide();

另外,您需要在对not()函数的第一个参数求值后过滤结果。 您可以通过将另一个not()函数链接到过滤器来做到这一点,例如:

$('.HoverChild').not( $(this).parent().closest('.HoverChild') )
                .not ($(this) )
                .hide();

或者,您可以使用add()$(this).parent().closest('.HoverChild')$(this)到单个jQuery集合中,然后在其上评估not()

var $not = $(this).parent().closest('.HoverChild').add( $(this) );
$('.HoverChild').not($not).hide();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM