检查是否< ul> <李>有一个活跃的链接? [英] check if <ul> <li> has an active link?

查看:91
本文介绍了检查是否< ul> <李>有一个活跃的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

<ul>
<li class="section-title">HEADER which triggers dropdown</li>
   <li><a href="element_one">element one</a></li>
   <li><a href="element_one">element one</a></li>
   <li><a href="element_one">element one</a></li>
</ul>

我用这一行隐藏了所有li元素(第一个除外):

i'm hiding all li-elements (except the first one) with this line:

$('#menu ul li.section-title:first-child').parent().children('li').hide();

我想知道是否可以查询这三个链接中的一个是否处于活动状态,如果是这样,则不应隐藏任何内容!

i wonder if it's possible to query if one of those three links is active and if so nothing should hide!

甚至可能吗?谢谢你的帮助!

is that even possible? thank you for your help!

推荐答案


  1. 你的代码实际上会隐藏所有 li
    个元素(包括第一个
    一个
    )。

    使用 $('#menu ul li:not(:first-child)')。hide(); 隐藏除第一个之外的所有..

  2. 为下一个part(仅隐藏它们,如果没有当前的)使用

  1. Your code will actually hide all li elements (including the first one).
    Use $('#menu ul li:not(:first-child)').hide(); to hide all but the first one..
  2. For the next part (to only hide them if none is the current one) use

var loc = window.location.href;
var anyActive = false;
$('#menu li a').each(function(){
    anyActive = anyActive || (this.href == loc);
});

if (!anyActive)
    $('#menu ul li:not(:first-child)').hide();


使用工作代码更新,之后评论

var loc = window.location.href;

$('#menu li a').each(function(){
    if (this.href == loc)
        $(this).addClass('activelink');
});

$('#menu ul:not(:has(.activelink)) li:not(:first-child)').hide();

实例: http://jsfiddle.net/gaby/SMmtS/21/

这篇关于检查是否&lt; ul&gt; &LT;李&GT;有一个活跃的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆