• 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
  • 相关阅读:
    行为型模式之 命令模式
    结构型模式之 代理模式
    oop编程思想
    2013应届毕业生各大IT公司待遇整理汇总篇(转)
    python定义class
    python——博客园首页信息提取与分析(转载有改动)
    深入浅出TCP/IP协议
    python基础之socket
    python基础
    c++stl之stack
  • 原文地址:https://www.cnblogs.com/liangshuang/p/9237552.html
Copyright © 2020-2023  润新知