繁体   English   中英

使用JQuery过滤以忽略父母的子女

[英]Filtering using JQuery to omit children of parents

我想从“弹出”类的所有元素中删除“活动”类,其中元素不在特定元素的父树中。 我以为会是这样的:

$(".active.popup").not($("#element").parents()).removeClass("active");

但是我感觉这里使用parents()是无效的吗?

HTML示例:-

<div>
    <div class="popup active">
        <!-- leave this active class - it is in parent tree of #element -->
        <div>
            <div class="popup active">
                <!-- leave this active class - it is in parent tree of #element -->
                <a id="#element"></a>
            </div>
        </div>
    </div>

    <div class="popup active">
        <!-- remove this active class - not in parent tree of #element -->
        <div>
            <div class="popup active">
                <!-- remove this active class - not in parent tree of #element -->
            </div>
        </div>
    </div>

</div>
<div>
    <div class="popup active">
        <!-- remove this active class - not in parent tree of #element -->
    </div>
</div>
</body>

 $(document).ready(function() { window.disableOnActive = function() { var element = document.getElementById("element"); $(".active.popup").each(function(i, v) { if ($(element.parentElement).has(v).length == 0) { $(v).removeClass("active"); } }) } }); 
 .active { color: orange; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="test"> <div> <div id="element"> <p class="active popup"> I am active but not in parent </p> </div> </div> <p class="active popup"> I am active outside the parent 1 </p> <p class="active popup"> I am active outside the parent 1 </p> </div> <button onClick="disableOnActive()">Disable Active </button> 

暂无
暂无

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

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