How does jq realize that the current element is clicked to hide other elements at the same level

<span id="jianjie addClass"></span>
<span id="lishi"></span>
<span id="rongyu"></span>


<script type="text/javascript">
$(function(){
    $("-sharplishi").click(function(){
        $(".lishi").addClass("animate");
        $(".jianjie").removeClass("animate");
    });

    $("-sharprongyu").click(function(){
        $(".rongyu").addClass("animate");
        $(".lishi").removeClass("animate");
    });

})   
</script>




it"s too cumbersome to write that

Mar.15,2021

<span id="jianjie addClass" class="list"></span>
<span id="lishi" class="list"></span>
<span id="rongyu" class="list"></span>
<script type="text/javascript">
$(function(){
    $(".list").click(function(){
        $(this).addClass("animate").siblings().removeClass("animate");
    });

})   
</script>

siblings ()


    In the event callback of
  1. jQ, $(this) is event.target , that is, the DOM object that triggers the event
  2. take a look at the "traversal" category of
    <script type="text/javascript">
    $(function(){
        $("-sharplishi").click(function(){
            $(this).addClass("animate").siblings().removeClass("animate");
        });
    
        $("-sharprongyu").click(function(){
            $(this).addClass("animate").siblings().removeClass("animate");
        });
    
    })   
    </script>

    it is suggested that nodes can be stored in a variable before use, and it is not efficient to find nodes repeatedly. To put it simply, the principle is that the this is currently hidden, and then traverse the sibling nodes to achieve the effect.


    Baidu knows


    by clicking "event delegate".

    you can use slblings () to select sibling nodes. Portal-go to JSRUN to see the effect

    <span id="jianjie" class="item"></span>
    <span id="lishi" class="item"></span>
    <span id="rongyu" class="item"></span>
    <script type="text/javascript">
    $(function(){
        $(".item").off().on('click',function(){
            $(this).addClass("animate").siblings().removeClass('animate')
        });
    })   
    </script>
    
    
Menu