jQuery 这个子元素悬停激活背景颜色

发布于 2024-12-18 18:38:16 字数 3949 浏览 2 评论 0原文

我仍在学习 jQuery,并且希望得到一些指导,因为我认为它很简单,但不确定如何做到这一点。

请参阅下面我由 wordpress 生成的标记,它是带有帖子计数的类别的侧栏列表。

    <div id"sidebar-left">

        <ul id="sidebar-categories">
            <li class="list-header">Contents</li>

            <li class="cat-item cat-item-3">
                <a title="" href="x">News</a>
                <span>1</span>
            </li>
            <li class="cat-item cat-item-4">
                <a title="" href="x">Racing</a>
                <span>4</span>
            </li>
            <li class="cat-item cat-item-1">
                <a title="" href="x">Uncategorized</a>
                <span>1</span>
            </li>
        </ul>

    </div>

现在这一切都很酷,但是我的跨度计数绝对位于 li 中 a 标签的顶部。

这意味着我的问题是,当我悬停 a 标签,然后悬停跨度时, a 标签的悬停状态消失。所以我试图激活 a 标签:当我滚动到跨度上时悬停。

现在我已经成功地使用下面的 jQuery 来做到这一点...

    $("ul#sidebar-categories li span").hover(function() {
        $("ul#sidebar-categories li a").css({ 
            backgroundColor: "464848" // a tag hover bg color
        });
    }, function() {
        $("ul#sidebar-categories li a").css({ 
            backgroundColor: "daddd7" // a tag normal bg color
        });
    });

但显然如您所见,这将激活此侧边栏类别列表中所有 a 标签的所有背景颜色。

我下面的尝试只是让它激活当前父 li 内的标签背景颜色。但这不起作用。我也尝试过父母,但仍然没有效果。

    $("ul#sidebar-categories li span").hover(function() {
        $(this).children("ul#sidebar-categories li a").css({ 
            backgroundColor: "464848" // a tag hover bg color
        });
    }, function() {
        $(this).children("ul#sidebar-categories li a").css({ 
            backgroundColor: "daddd7" // a tag normal bg color
        });
    }); 

另请参阅下面的列表 ui 的屏幕截图,以便您了解为什么我需要它以这种方式工作。

Hover Status Category List UI

因此,当您将鼠标悬停在类别列表上,然后将鼠标悬停在“帖子计数”上时,a:悬停伪类丢失。

提前致谢。 乔希

//// 添加我的 CSS

#sidebar-left ul {
    font-family: 'HelveticaNeueMediumCond';
    position: relative;
    border-bottom: 1px solid #eef0eb;
    padding: 21px 0 0;
    list-style: none;
}

#sidebar-left ul li {
    position: relative;
    text-transform: uppercase;
    padding: 0;
    margin: 0;
    overflow: hidden;
    display: block;
    height: 48px;
}

.list-header {
    border-top: 1px solid #eef0eb !important;
    border-bottom: 1px solid #c6c8c2 !important;
    height: 15px !important;
    padding: 7px 10px 5px !important;
    background: #c4c7c2;
    color: #fbfcfb;
    font-size: 14px;
}

#sidebar-left ul li a {
    width: 270px;
}

#sidebar-left ul li a {
    height: 20px;
    display: inline-block;
    border-top: 1px solid #eef0eb;
    border-bottom: 1px solid #c6c8c2;
    text-decoration: none;
    font-size: 20px;
    padding: 13px 10px;
    color: #464848;
    text-shadow: #eef0ea 1px 1px 0;
    outline: 0;
}

#sidebar-left ul li a:hover {
    border-top: 1px solid #4c4e4c;
    border-bottom: 1px solid #464848;
    text-decoration: none;
    color: #fbfcfb;
    text-shadow: #2d2e2e 1px 1px 0;
    background: #464848;
}

#sidebar-left ul li span {
    position: absolute;
    right: 10px;
    background: #ffffff;
    padding: 4px 0;
    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
    border-radius: 7px;
    font-size: 15px;
    color: #2cbcf5;
    height: 15px;
    min-width: 24px;
    text-align: center;
    z-index: 2;
    top: 0;
    font-family: 'HelveticaNeueBoldCond';
    margin: 12px 0;
    -webkit-box-shadow: 0px 1px 0px 0px #cbcec8;
    -moz-box-shadow: 0px 1px 0px 0px #cbcec8;
    box-shadow: 0px 1px 0px 0px #cbcec8; 
}

#sidebar-left ul li span:hover {
    -webkit-box-shadow: 0px 1px 0px 0px #252525;
    -moz-box-shadow: 0px 1px 0px 0px #252525;
    box-shadow: 0px 1px 0px 0px #252525;    
}

I'm still learning jQuery and would appreciate some pointers with this, as I think its simple but not sure how to do it quite.

See below my mark up generated by wordpress, its a side bar list for categorys with post count.

    <div id"sidebar-left">

        <ul id="sidebar-categories">
            <li class="list-header">Contents</li>

            <li class="cat-item cat-item-3">
                <a title="" href="x">News</a>
                <span>1</span>
            </li>
            <li class="cat-item cat-item-4">
                <a title="" href="x">Racing</a>
                <span>4</span>
            </li>
            <li class="cat-item cat-item-1">
                <a title="" href="x">Uncategorized</a>
                <span>1</span>
            </li>
        </ul>

    </div>

Now this is all cool, but my span count is positioned absolute over the top of the a tag in the li.

Which means my problem is, when I hover my a tag, and then hover the span, my hover state of the a tag disappears. So I'm trying to activate the a tag :hover when I roll over the span.

Now I have managed to do this using the jQuery below...

    $("ul#sidebar-categories li span").hover(function() {
        $("ul#sidebar-categories li a").css({ 
            backgroundColor: "464848" // a tag hover bg color
        });
    }, function() {
        $("ul#sidebar-categories li a").css({ 
            backgroundColor: "daddd7" // a tag normal bg color
        });
    });

But obviously as you can see, this will activate all the background colors of all the a tags in this sidebar-categories list.

This my attempt below to only get it to activate the a tag background color inside the current parent li. But it's not working. I've also tried parent but still no worky.

    $("ul#sidebar-categories li span").hover(function() {
        $(this).children("ul#sidebar-categories li a").css({ 
            backgroundColor: "464848" // a tag hover bg color
        });
    }, function() {
        $(this).children("ul#sidebar-categories li a").css({ 
            backgroundColor: "daddd7" // a tag normal bg color
        });
    }); 

Also see screen shot below of the list ui so you can see why I need it to work this way.

Hover Status Category List UI

So as your hovering the category list, and then mouse over the 'post count', the a:hover pseudo class is lost.

Thanks in advance.
Josh

//// Addition my CSS

#sidebar-left ul {
    font-family: 'HelveticaNeueMediumCond';
    position: relative;
    border-bottom: 1px solid #eef0eb;
    padding: 21px 0 0;
    list-style: none;
}

#sidebar-left ul li {
    position: relative;
    text-transform: uppercase;
    padding: 0;
    margin: 0;
    overflow: hidden;
    display: block;
    height: 48px;
}

.list-header {
    border-top: 1px solid #eef0eb !important;
    border-bottom: 1px solid #c6c8c2 !important;
    height: 15px !important;
    padding: 7px 10px 5px !important;
    background: #c4c7c2;
    color: #fbfcfb;
    font-size: 14px;
}

#sidebar-left ul li a {
    width: 270px;
}

#sidebar-left ul li a {
    height: 20px;
    display: inline-block;
    border-top: 1px solid #eef0eb;
    border-bottom: 1px solid #c6c8c2;
    text-decoration: none;
    font-size: 20px;
    padding: 13px 10px;
    color: #464848;
    text-shadow: #eef0ea 1px 1px 0;
    outline: 0;
}

#sidebar-left ul li a:hover {
    border-top: 1px solid #4c4e4c;
    border-bottom: 1px solid #464848;
    text-decoration: none;
    color: #fbfcfb;
    text-shadow: #2d2e2e 1px 1px 0;
    background: #464848;
}

#sidebar-left ul li span {
    position: absolute;
    right: 10px;
    background: #ffffff;
    padding: 4px 0;
    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
    border-radius: 7px;
    font-size: 15px;
    color: #2cbcf5;
    height: 15px;
    min-width: 24px;
    text-align: center;
    z-index: 2;
    top: 0;
    font-family: 'HelveticaNeueBoldCond';
    margin: 12px 0;
    -webkit-box-shadow: 0px 1px 0px 0px #cbcec8;
    -moz-box-shadow: 0px 1px 0px 0px #cbcec8;
    box-shadow: 0px 1px 0px 0px #cbcec8; 
}

#sidebar-left ul li span:hover {
    -webkit-box-shadow: 0px 1px 0px 0px #252525;
    -moz-box-shadow: 0px 1px 0px 0px #252525;
    box-shadow: 0px 1px 0px 0px #252525;    
}

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

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

发布评论

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

评论(1

时间海 2024-12-25 18:38:16

是否有原因无法激活父列表项上的悬停,而不是锚标记?如果这不起作用,您可以使用 .siblings() 函数来选择跨度的所有同级 DOM 元素。尝试这样的操作:

$("ul#sidebar-categories li span").hover(function() {
    $(this).siblings().css({ 
        backgroundColor: "464848" // a tag hover bg color
    });
}, function() {
    $(this).siblings().css({ 
        backgroundColor: "daddd7" // a tag normal bg color
    });
}); 

这将选择任何直系兄弟并更改其背景颜色。如果需要进一步过滤元素,可以将 CSS 选择器传递给 .siblings() 函数。

Is there a reason you can't activate the hover on the parent list items, rather than the anchor tag? If that doesn't work, you can use the .siblings() function to select all sibling DOM elements of the span. Try something like this:

$("ul#sidebar-categories li span").hover(function() {
    $(this).siblings().css({ 
        backgroundColor: "464848" // a tag hover bg color
    });
}, function() {
    $(this).siblings().css({ 
        backgroundColor: "daddd7" // a tag normal bg color
    });
}); 

This will select any immediate siblings and change their background color. If you need to further filter the elements, you can pass a CSS selector to the .siblings() function.

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