1

I have a working demo of breadcrumbs here in this JSFIDDLE

Below is the code

HTML

<div id="crumbs">
    <ul>
        <li><a href="#1">One</a></li>
        <li><a href="#2">Two</a></li>
    </ul>
</div>

CSS

#crumbs ul li {
    display: inline;
}

#crumbs ul li a {
    display: block;
    float: left;
    height: 50px;
    background: #3498db;
    text-align: center;
    padding: 30px 40px 0;
    position: relative;
    margin: 0 10px 0 0;
    font-size: 20px;
    text-decoration: none;
    color: #fff;
    padding: 30px 40px 0 80px;
}

#crumbs ul li a:after {
    content: "";
    border-top: 40px solid rgba(0,0,0,0);
    border-bottom: 40px solid rgba(0,0,0,0);
    border-left: 40px solid #3498DB;
    position: absolute;
    right: -40px;
    top: 0;
    z-index: 1;
}

#crumbs ul li a:before {
    content: "";
    border-top: 40px solid rgba(0,0,0,0);
    border-bottom: 40px solid rgba(0,0,0,0);
    border-left: 40px solid #fff;
    position: absolute;
    left: 0;
    top: 0;
}

#crumbs ul li a:hover {
    background: #fa5ba5;
}

#crumbs ul li a:hover:after {
    border-left-color: #fa5ba5;
}

Css has pink color for mouse hover but what i actually want to do is to preserve that color once any item on breadcrumbs is clicked ie: the pink color should remain after a click which can act as a signal to user as of which tab in breadcrumb is currently active.

I have tried using :active in css but it does not preserve the color on breadcrumbs after click.

Any help ?

4

1 回答 1

1

你必须使用:visited选择器。像这样的东西会很好用:

#crumbs ul li a:visited {
  background-color: #fa5ba5;
}

#crumbs ul li a:visited:after {
    border-left-color: #fa5ba5;
}

还有 JS Fiddle:演示

编辑(完整的 JS 方法):

您可以在此处的 JSFiddle中找到完整的 JS 演示。在onclick事件中,有一个循环.onclick从先前的活动链接中删除类。

提供的代码必须改进,这只是为了向您展示它可以工作。

我强烈推荐你使用像 jQuery 这样的库,因为在原生 JS 代码中的 DOM 操作非常无聊。

为了向您展示差异,使用 jQuery 您必须这样做:

HTML 代码:

<div id="crumbs">
    <ul>
        <li><a class="link" href="#1">One</a></li>
        <li><a class="link" href="#2">Two</a></li>
    </ul>
</div>

JS代码:

$('.link').on('click', function() {   
    $('.onclick').removeClass('onclick'); // Remove class from previous active link
    $(this).addClass('onclick'); // Add class to new active link 
});
于 2013-09-24T08:44:46.380 回答