简体   繁体   中英

Nested Ul LI hover hide show element jquery

In nested ul li, I need to show edit and update link on li hover. I have jquery code that does it for me. Jquery works fine when i traverse from top to bottom but when I traverse from bottom to top, it does not work as desired and shows parent li's hidden div.

I want only hover li's span to show. Here is the required.

<ul id="tree">
<li><span>Mobile </span>&nbsp; <span class="links" style="display: none;">Hey Hi, YOu
    caught me!!!</span>
    <ul class="subItem">
        <li><span>GSM Mobiles </span>&nbsp; <span class="links" style="display: none;">Hey Hi,
            YOu caught me!!!</span> </li>
        <li><span>Smart Mobiles </span>&nbsp; <span class="links" style="display: none;">Hey
            Hi, YOu caught me!!!</span>
            <ul class="subItem">
                <li><span>Android Mobiles </span>&nbsp; <span class="links" style="display: none;">Hey
                    Hi, YOu caught me!!!</span> </li>
                <li><span>Sabian Mobiles </span>&nbsp; <span class="links" style="display: none;">Hey
                    Hi, YOu caught me!!!</span> </li>
            </ul>
        </li>
        <li><span>Dual SIM Mobiles </span>&nbsp; <span class="links" style="display: none;">
            Hey Hi, YOu caught me!!!</span> </li>
    </ul>
</li>
<li><span>Watches </span>&nbsp; <span class="links" style="display: none;">Hey Hi, YOu
    caught me!!!</span>
    <ul class="subItem">
        <li><span>Chronograph Watches </span>&nbsp; <span class="links" style="display: none;">
            Hey Hi, YOu caught me!!!</span> </li>
        <li><span>Water Resistance </span>&nbsp; <span class="links" style="display: none;">
            Hey Hi, YOu caught me!!!</span> </li>
    </ul>
</li>
</ul>

My jQuery Code is here:

$('ul li').hover(function () {
            $("ul>li>span.links").hide();
            $(this).find("span.links").first().show();
        }, function () {

            $("ul>li>span.links").hide();
            $(this).find("span.links").first().hide();

        });

Here is jsFiddle Link

When printing out $(this) I noticed that, you are selecting both the inner and outer lists, be more specific in your selector:

$('#tree ul li').hover(function () {
   $("ul>li>span.links").hide();  

    $(this).find("span.links").first().show();
}, function () {
    $("ul>li>span.links").hide();
    $(this).find("span.links").first().hide();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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