简体   繁体   English

OnClick函数无法获取一个块的“值”属性的值,但可以获取另一块的值

[英]OnClick function cannot get value of one block's 'value' prop, but it can get the value of another block

In App.js I have changePage() function which sets stage of App's state . 在App.js中,我具有changePage()函数来设置App state阶段。 This state is used by addContent() function to render main content of the application: addContent()函数使用此state来呈现应用程序的主要内容:

    class App extends Component {
        constructor(props) {
            super(props);
            this.state = {
               content: "main"
            };
        }

    changePage(pageName) {
        this.setState({
            content: pageName
        });
    }

    addContent() {
        if (this.state.content === "main") {
            return(
                <Main />
            );
        } else if (this.state.content === "blog") {
            return (
                <Blog />
            );
        } else {
            console.log("Reference error: Invalid page.");
        };
    }

    render() {
        return (
            <div className="App">
                <Header changePage={ this.changePage.bind(this) } />
            </div>
        )
    }
}

This function is given to Header component as prop named changePage . 此功能作为名为changePage prop提供给Header组件。 And there it is handed to two different components: NavLogo and NavBar : 然后将其交给两个不同的组件: NavLogoNavBar

class Header extends Component {
    render() {
        return (
            <header id="moving-header">
                <NavLogo changePage={ this.props.changePage } />
                <NavBar changePage={ this.props.changePage } />
            </header>
        )
    }
}

From NavBar to NavItem : NavBarNavItem

class NavBar extends Component {
    render() {
        return (
            <ul className="nav">
                <NavItem 
                    isActive = {true}
                    buttonType = "bt"
                    page = "main"
                    area = "#home"
                    changePage = {this.props.changePage}
                    itemName="home" 
                />
                <NavItem 
                    isActive = {false}
                    buttonType = "blog"
                    page = "blog"
                    changePage = {this.props.changePage}
                    itemName="blog" 
                />
            </ul>
        );
    }
}

It worsk flawless for NavItem elements: 对于NavItem元素,它完美无瑕:

class NavItem extends Component {
    constructor(props) {
        super(props);

        this.handlePageChange = this.handlePageChange.bind(this);
    }

    handlePageChange(e) {
        this.props.changePage(e.target.value);
    }

    render() {
        return (
        <li isActive={ this.props.isActive /*Apply certain styles on active button*/}>
            <button
                className={ this.props.buttonType /*bn or blog*/} 
                value={ this.props.page /*main or blog*/} 
                href={ this.props.area /*optional - anchor to section on Main page*/ }


          onClick={ this.handlePageChange /*For all buttons - handles change of page*/}>
        { this.props.itemName /*Name of button for User*/}</button>
    </li>
    );
}

} }

But it completely doesn't for NavLogo : 但这完全不适用于NavLogo

class NavLogo extends Component {
    constructor(props) {
        super(props);

        this.handlePageChange = this.handlePageChange.bind(this);
    }

    handlePageChange(e) {
        this.props.changePage(e.target.value);
        console.log(this.props.changePage);
        console.log(e.target);
        console.log(e.target.value);
    }

    render() {
        return (
            <button className="logo-button">
                <div className="logom" value="main" onClick={ this.handlePageChange } ></div>
            </button> 
        );
    }
}

As it may be seen from console.log statements, it gets this.props.changePage , it knows e.target , but only in case of NavLogo ,'s #logom element, it cannot obtain it's e.target.value prop's value. console.log语句可以看出,它获取this.props.changePage ,它知道e.target ,但是仅在NavLogo#logom元素的情况下,它无法获取其e.target.value道具的值。 I cannot figure out why for a couple of days wandering through my code and re-writing it. 我想不通为什么要花几天时间浏览我的代码并重新编写它。 Could someone help me find the reason? 有人可以帮我找到原因吗? I'll give you all my gratitude! 我将感激不尽!

Having a div inside a button is semantically incorrect. 在按钮内使用div在语义上是不正确的。

<button className="logo-button">
    <div className="logom" value="main" onClick={ this.handlePageChange } ></div>
</button>

Change the syntax to a valid one and use the proper way of getting the value. 将语法更改为有效的语法,并使用获取值的正确方法。 If you want to use a div you may want to add a data attribute to it. 如果要使用div,则可能要向其添加data属性。 Something like data-value and use this as a value. 诸如数据值之类的东西,并将其用作值。

value attribute is not a valid attribute for the <div> tag, and hence it is unavailable from event.target directly, you could get it like e.target.getAttribute('value') value属性不是<div>标签的有效属性,因此无法直接从event.target获得,您可以像e.target.getAttribute('value')一样获得它

The value attribute is available on following value属性在以下位置可用

<button>, <option>, <input>, <li>, <meter>, <progress>, <param>

Check this documentation for more details 查看文档以了解更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM