嵌套的Ul LI悬停隐藏显示元素jquery

时间:2012-08-03 06:48:39

标签: javascript jquery html

在嵌套文件中,我需要在li hover上显示编辑和更新链接。我有jquery代码,它为我做。当我从上到下遍历时,Jquery工作正常但是当我从下到上遍历时,它不能按预期工作并显示父li的隐藏div。

我只想悬停李的跨度来展示。这是必需的。

<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>

我的jQuery代码在这里:

$('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();

        });

以下是 jsFiddle 链接

1 个答案:

答案 0 :(得分:1)

打印出$(这个)时我注意到了, 您正在选择内部和外部列表,在选择器中更具体:

$('#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();
});