• react 给选中的li添加样式


    思路:使用事件委托,
    关键:获取到的index必须转为数字,因为它是字符串
    handleClick = (e) => {
    const nodeName = e.target.nodeName.toUpperCase()
    let tag = e.target;
    if (nodeName === 'LI') {
    let index = parseInt(tag.getAttribute('index'))
    this.setState({
    currentIndex: index
    })
    }
    }


    import React from 'react'
    import './nav.scss'

    class NavCom extends React.Component {
    constructor(props) {
    super(props)
    this.state = {
    currentIndex: 0
    }
    }
    sestCurrentStyle = (index) => {
    return this.state.currentIndex === index ? 'current' : ''
    }
    handleClick = (e) => {
    const nodeName = e.target.nodeName.toUpperCase()
    let tag = e.target;
    if (nodeName === 'LI') {
    let index = parseInt(tag.getAttribute('index'))
    this.setState({
    currentIndex: index
    })
    }
    }
    render() {
    const navList = this.props.navList
    return (
    <div className='nav-wrap' onClick={(e)=>this.handleClick(e)}>
    {
    navList && navList.map( (item, index) => {
    return (
    <li key={index} index={index} className={this.state.currentIndex === index ? 'current' : ''}>{item}</li>
    )
    })
    }
    </div>
    )
    }
    }
    export default NavCom
  • 相关阅读:
    51Nod 1239 欧拉函数之和
    51Nod 1244 莫比乌斯函数之和
    BZOJ 4805: 欧拉函数求和
    BZOJ 3944: Sum
    3.25阅读摘抄
    生活整洁之道
    1064. 朋友数(20)
    1063. 计算谱半径(20)
    1061. 判断题(15)
    1062. 最简分数(20)
  • 原文地址:https://www.cnblogs.com/liangshuang/p/9237552.html
Copyright © 2020-2023  润新知