【问题标题】:How do I make an li change colour when active?激活时如何使li改变颜色?
【发布时间】:2020-04-19 15:54:45
【问题描述】:

我正在我的网站上创建一个常见问题解答部分,您应该可以通过单击其中一个部分主题来过滤问题。 I have this functioning however what I want is for when one of the section titles is selected, that specific title should change from being red to black and underlined.当鼠标被按住时,它可以工作,但一旦我放开它,它就会变回来。当某个部分处于活动状态时,如何让它保持这种状态?

CSS:

#portfolio .portfolio-list .nav li {
 margin: 0 40px 0 0;
 float: left;
 color: #C42B32;
 line-height: 16px;
 cursor: pointer;
 font-size: 20px;
 font-weight: 600 ! important;
 font-family: 'Assistant', sans-serif ! important;
 letter-spacing: 0.02em;
 -moz-transition: all 0.5s ease-in-out 0s;
 -ms-transition: all 0.5s ease-in-out 0s;
 -o-transition: all 0.5s ease-in-out 0s;
 -webkit-transition: all 0.5s ease-in-out 0s;
 transition: all 0.5s ease-in-out 0s;
 text-transform: uppercase;
}

#portfolio .portfolio-list .nav li:hover, #portfolio .portfolio-list .nav li.filter-active {
 color: #000000;
 -moz-transition: all 0.5s ease-in-out 0s;
 -ms-transition: all 0.5s ease-in-out 0s;
 -o-transition: all 0.5s ease-in-out 0s;
 -webkit-transition: all 0.5s ease-in-out 0s;
 transition: all 0.5s ease-in-out 0s; }

#portfolio .portfolio-list .nav li:active, li:focus, li:focus-within {
color: #000000 ! important;
text-decoration: underline;
}

标题:

import React from 'react';

export default class Filters extends React.Component {

    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div className="portfolio-list">
                <ul className="nav list-unstyled" id="portfolio-flters">
                    <li className="faqnav" data-item="all" onClick={this.props.filter}>All</li>
                    <li className="faqnav" data-item="general" onClick={this.props.filter}>General</li>
                    <li className="faqnav" data-item="stats" onClick={this.props.filter}>Stats</li>
                    <li className="faqnav" data-item="account" onClick={this.props.filter}>Account</li>
                    <li className="faqnav" data-item="social" onClick={this.props.filter}>Social Media</li>
                </ul>
            </div>
        );
    }
}[![Screenshot][1]][1]

【问题讨论】:

    标签: html css filter nav reactjs


    【解决方案1】:

    尝试以下方法: 从“反应”导入反应;

    export default class Filters extends React.Component {
    
        constructor(props) {
            super(props);
        }
    
        state = {
            activeFilter: 0,
        }
    
        render() {
            return (
                <div className="portfolio-list">
                    <ul className="nav list-unstyled" id="portfolio-flters">
                        <li className={`faqnav ${this.state.activeFilter === 1 ? 'active': ''}`}  data-item="all" onClick={(e) => this.filter(e, 1)}>All</li>
                        <li className={`faqnav ${this.state.activeFilter === 2 ? 'active': ''}`}  data-item="general" onClick={(e) => this.filter(e, 2)}>General</li>
                        <li className={`faqnav ${this.state.activeFilter === 3 ? 'active': ''}`}  data-item="stats" onClick={(e) => this.filter(e, 3)}>Stats</li>
                        <li className={`faqnav ${this.state.activeFilter === 4 ? 'active': ''}`}  data-item="account" onClick={(e) => this.filter(e, 4)}>Account</li>
                        <li className={`faqnav ${this.state.activeFilter === 5 ? 'active': ''}`}  data-item="social" onClick={(e) => this.filter(e, 5)}>Social Media</li>
                    </ul>
                </div>
            );
        }
    
        filter = (e, filterId) => {
            this.setState({
                activeFilter: filterId,
            });
            this.props.filter(e);
        }
    }
    

    CSS:

    .faqnav.active {
        color: #000;
        text-decoration: underline;
    }
    

    说明: 添加一个本地状态,保存当前活动的过滤器。为每个过滤器分配某种 ID,我建议您使用常量来执行此操作。之后,每次单击过滤器时,将单击的过滤器 id 保存在状态中并执行常规的 onClick 处理程序。然后在每个&lt;li&gt; 中为当前活动的过滤器添加一个检查并添加一个活动类。

    希望对您有所帮助。

    【讨论】:

    • 当我使用上面的代码然后选择其中一个过滤器时,我得到一个'TypeError:无法读取未定义的属性'目标'以及'匿名函数)src/components/Faqquestions2.js: 23 20 |让filteredItems = []; 21 | 22 | this.state.items.forEach(function(item) { > 23 | if (item.tags.includes(event.target.dataset.item)) { | ^ 24 | filteredItems.push(item); 25 | } 26 | });'
    • 我更新了答案。我不知道您使用的是 event 参数,因为您没有通过 props 呈现原始 filter() 函数。请尝试新的解决方案。
    • 完美的工作!现在唯一不变的是颜色?下划线保持不变,但颜色恢复为红色而不是保持黑色......
    • 如果需要,通过添加您列出的所有类来向我提供的 CSS 类添加更多规范,因为您以前的样式可能会覆盖我的代码。还要确保,我上的课放在你的课之后。如果这仍然不起作用,请尝试添加!important (在这种情况下还不错,因为活动类用于覆盖常规类样式)。
    • 非常感谢您的帮助,非常感谢!
    猜你喜欢
    • 2020-01-24
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2017-08-21
    • 2015-04-27
    • 2023-04-10
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多