使用jQuery在菜单中自动选择li

时间:2014-07-31 11:52:08

标签: jquery

我有一个下拉菜单,它在悬停时使用jQuery。您可以查看侧栏中的here

我使用的jQuery代码是

    $(document).ready(function(){
         $( ".sidebarnav ul.sf-menu" ).find('li').hover(function() {
                $(this).find('> ul').toggle("slow");
         },
         function() {
            $(this).find('> ul').toggle("slow");
         });
    });

现在我有一个查询。我希望根据在此页面上打开的产品选择li。这可以通过在菜单中找到li文本来完成。

如果我有价值的李说" Wing Banner"。然后,我可以打开确切的li" Wing Banner"在侧边栏菜单中。

3 个答案:

答案 0 :(得分:2)

根据您的问题,我发现您需要根据product_title entry-title

中的文字打开侧栏

你必须找到li所在的ul,所以在li示例中写下每个函数

 $("li").each(function(){
 alert($(this).text());
 if(the text is same)
 {
 find parent using parent() (i.e) the ul and toggle it
 }
 });

我认为当用户将鼠标悬停在over ul元素上时,你必须再次切换它 所以再次添加切换功能,以便你的案例横幅和标志中的旧切换器将会消失。

答案 1 :(得分:0)

是的,你可以这样做:

步骤1:最初找到正在内容中加载的页面,在您的情况下,它是“Wing Banner”,它位于p tag with class productpage_title。使用jquery查找页面标题(确保侧边菜单中的页面标题和标题是相同的,否则你必须使用其他方式来查找正在加载的页面)。

步骤2:然后在侧边栏中,使用jquery,您可以找到li所在的ul,应用css来显示li(应用显示li的当前类悬停)

使用基本fiddle更新了我的内容。试试吧。

 $('#cssmenu li a').each(function(){ 
    if($(this).text() == $('#your_id').text()){ 
        $(this).css('background-color','blue').closest('ul').show();
    }
});

从我的小提琴中,在滑动条上执行每个功能以检查,如果它有你需要的标题,那么它是否只是显示它。

答案 2 :(得分:0)

请按照下面的课程进行,然后使用脚本

.selected-menu {background-color:#eee;}

<script> 
    $(document).ready(function(){

      $( ".sidebarnav ul.sf-menu li a" ).click(function() {
         $( ".sidebarnav").find(".selected-menu").removeClass(".selected-menu");
         $(this).parents("li").addClass(".selected-menu")

     });


   });

</script>