本文介绍了搜索具有共同祖先的物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个html:

<div>
    <div><!-- all div without ID -->
        <span>some text</span>
        <div>
          <span id="listener1">click here</span>
          <span>sometext</span></div>
        <div>

        <span class="FIND_ME">Result Here</span></div>
    </div>

    <div>
        <span>some text</span>
        <div id="div1">
         <div id="div2">
          <span id="listener2">click here</span>
          <span>sometext</span></div>
         </div>
        <div>

        <span class="FIND_ME">Result Here</span></div>
    </div>
</div>

应为以下逻辑:当我单击单击此处"时,应隐藏类为"FIND_ME"的元素,这是按下按钮时最接近的共同祖先.是否有可能做到这一点?

Should be the following logic: wheen I click on "click here" the element with class "FIND_ME" should hide, which is the nearest common ancestor with the button pressed. Is it possible to do this?

$("#listener1").click(function(){
    $(this).<SUPER_SEARCHING>.hide(); // for example hide, or add some CSS class
});

推荐答案

您可以找到其中包含元素.find_me的最接近的div:

You can find closest div that has element .find_me in it:

$("#listener1").click(function(){
   $(this).closest('div:has(.FIND_ME)').find('.FIND_ME').hide();
});

工作演示

Working Demo

这篇关于搜索具有共同祖先的物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 04:56