我一直在嘗試在我的系統(tǒng)中實(shí)現(xiàn)一個(gè) onclick 模式關(guān)閉。我已經(jīng)使用關(guān)閉按鈕實(shí)現(xiàn)了一個(gè)模態(tài) onclick 事件,但是在模態(tài)外部單擊時(shí)沒有成功我已經(jīng)嘗試在 div 上添加 onclick 偵聽器,但即使在模態(tài)窗口內(nèi)單擊,內(nèi)容也會關(guān)閉。<div className={"Overlay " + (this.state.hidden? "hidden": "show")} id={this.props.id + "-container"} onClick={() => { this.setState({ hidden: true }) }}>到目前為止,這是我的模態(tài)框渲染代碼 render() { const contentClassName = this.getContentClassName(); if (this.props.show) { document.body.style.overflow = 'hidden'; } const contentStyle = { width: this.props.width, height: this.props.height, position: "relative" }; return ( <div className={"Overlay " + (this.state.hidden? "hidden": "show")} id={this.props.id + "-container"} onClick={() => { this.setState({ hidden: true }) }}> <div className={contentClassName}> <div className={"Overlay-container"} style={contentStyle}> <a id={this.props.id + '-closeButton'} className="Overlay-closeBtn icon-close-zoom" onClick={() => { this.setState({ hidden: true }) }}/> {this.props.children} </div> </div> </div> ); }正如預(yù)期的那樣,我希望在模態(tài)窗口外單擊時(shí)關(guān)閉模態(tài)。目前,即使在單擊模態(tài)窗口后它也會關(guān)閉。
在外部單擊時(shí)實(shí)現(xiàn)模態(tài)關(guān)閉
胡說叔叔
2021-06-10 05:01:02