Parent() 也抓取兄弟姐妹

发布于 2024-11-04 23:32:59 字数 2082 浏览 8 评论 0原文

在下页的示例中,我希望将“active”类应用于活动链接的父级:

http ://socalragdolls.com/jquerytest.html

<div id="topnav">
    <ul id="mainmenu">
        <li class="top" id="home"><a href="/">Home</a></li>
        <li class="top"><a href="kittens">Kittens</a>
            <ul id="mainmenu">
                <li><a href="caring">Caring for a Ragdoll</a></li>
                <li><a href="kittens">Current Litter</a></li>
                <li><a href="jquerytest.html">Future Litters</a></li>
                <li><a href="parents">Parents</a></li>
                <li><a href="pricing">Pricing</a></li>
                <li><a href="reserving">Reserving a Kitten</a></li>
            </ul>
        </li>
        <li><a href="about">About Us</a>
            <ul>
                <li><a href="about">Cattery</a></li>
                <li><a href="proprietor">Proprietor</a></li>
                <li><a href="testimonials">Testimonials</a></li>
            </ul>
        </li>
        <li class="last"><a href="contact.html">Contact Us</a></li>
    </ul>
</div>

本质上,当访问者在页面上(jquerytest.html)时,我希望父级“Kittens”也处于活动状态。

这是当前的 jQuery:

<script>
    $("#topnav a").each(function() {
        if(this.href == window.location || this.href == document.location.protocol + "//" + window.location.hostname + window.location.pathname)
        $(this).parent('li').addClass('active');
    });
</script>

<script>
    $(document).ready(function() {
        $('li.active').parent().parent().addClass('active');
    });
</script>

如您所见,这突出显示了子 .active li 以及父级的所有兄弟姐妹,这是我绝对不想要的。

有什么想法吗?

In the example on the following page, I would like the "active" class to apply to the parent of the active link:

http://socalragdolls.com/jquerytest.html

<div id="topnav">
    <ul id="mainmenu">
        <li class="top" id="home"><a href="/">Home</a></li>
        <li class="top"><a href="kittens">Kittens</a>
            <ul id="mainmenu">
                <li><a href="caring">Caring for a Ragdoll</a></li>
                <li><a href="kittens">Current Litter</a></li>
                <li><a href="jquerytest.html">Future Litters</a></li>
                <li><a href="parents">Parents</a></li>
                <li><a href="pricing">Pricing</a></li>
                <li><a href="reserving">Reserving a Kitten</a></li>
            </ul>
        </li>
        <li><a href="about">About Us</a>
            <ul>
                <li><a href="about">Cattery</a></li>
                <li><a href="proprietor">Proprietor</a></li>
                <li><a href="testimonials">Testimonials</a></li>
            </ul>
        </li>
        <li class="last"><a href="contact.html">Contact Us</a></li>
    </ul>
</div>

Essentially, when a visitor is on the page (jquerytest.html), I want the parent li "Kittens" to be active as well.

Here's the current jQuery:

<script>
    $("#topnav a").each(function() {
        if(this.href == window.location || this.href == document.location.protocol + "//" + window.location.hostname + window.location.pathname)
        $(this).parent('li').addClass('active');
    });
</script>

<script>
    $(document).ready(function() {
        $('li.active').parent().parent().addClass('active');
    });
</script>

As you can see, this highlights all siblings of the child .active li along with the parent, which I definitely don't want.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沫尐诺 2024-11-11 23:32:59

它不会选择所有内容。

它仅选择父级。问题在于 .active 的 CSS 规则,它对整个 li 进行样式设置(并且由于 ulli 的子级看起来所有兄弟姐妹都被选中了)。

只需为 .active ul{background-color:white;} 创建一个 css 规则。


如果问题在于红色边框而不是背景颜色,那么罪魁祸首就是这部分

$(document).ready(function() {
    $('li ul li.active').siblings().css('border', '1px solid red');
    $('li ul li.active').siblings().css('background-color', '#333');
});

,但这太明显了是真正的问题..

It does not select everything..

It selects just the parent. The problem is with your CSS rule for .active which styles the whole li (and since the ul is a child of the li it looks like all siblings are selected).

Just create a css rule for .active ul{background-color:white;}


If the issue lies with the red border and not the background-color then the culprit is this part

$(document).ready(function() {
    $('li ul li.active').siblings().css('border', '1px solid red');
    $('li ul li.active').siblings().css('background-color', '#333');
});

but that is too obvious to be the real problem..

吐个泡泡 2024-11-11 23:32:59

看起来您正在将样式应用于整个 ul,这就是它也“更改”其所有子项的原因...您可能需要重置 background-colorul 上使用 CSS。

It seems like you are applying the style to the whole ul, that's why it "changes" all its children as well... You might need to reset the background-color on ul using CSS.

贪恋 2024-11-11 23:32:59

我认为你的第二个脚本应该是这样的:

编辑

$('li.active').closest("li").find("a").addClass('active');

因此,我们只将 active 类添加到锚点。发生的情况是,您将班级添加到整个李中,其中包括孩子。

希望这有帮助
干杯

I think your second script should be like this:

EDIT

$('li.active').closest("li").find("a").addClass('active');

So with this we only add the active class to the anchor. What was happening is that you're adding the class to the whole li, which include children.

Hope this helps
Cheers

反话 2024-11-11 23:32:59

它实际上并不是突出显示同级,而是突出显示父 li 及其内部的所有内容。

我会将 active 类放在 kitten 链接上,或者您也可以向父级添加不同的类名,例如 active-trail

It's not actually highlighting the siblings, it is highlighting the parent li and all the content inside it.

I would put the active class on the kitten link instead, or you might add a different class name, like active-trail to the parent.

[浮城] 2024-11-11 23:32:59

正如Paul所说,父级的整个li都有背景,它封装了子级ul。避免这种情况的一种方法是使用背景图像,该图像仅与父链接一样高,并且不会在 y 轴上重复。

As Paul says, the whole li of the parent has the background, which encapsulates the child ul. One way to avoid this would be to use a background image, which is only as tall as the parent link, and does not repeat on the y axis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文