[英]Make a button clickable inside a drawer?
Hi I have a div
when someone clicks on the div
it just toggles hide/show like a drawer
If the drawer is in show state it has a button.您好我有一个div
时,在有人点击div
它只是切换隐藏/显示像drawer
出票人是在演艺状态时,它有一个按钮。 How to make the button clickable ie (If someone clicks on the button it's function should execute but the drawer should not toggle) below is my code any idea !!如何使按钮可点击,即(如果有人点击按钮,它的功能应该执行但抽屉不应该切换)下面是我的代码任何想法! `zIndex in not working `zIndex 不工作
<div onClick={this.toggleDiv.bind(this, "divid")} >
<div>
<div className="hide" id="divid" style={{zIndex: "1"}}>
<br />
<button onClick={this.divRemainShowAndAlertHi(this, plateInfo)}>
click me
</button>
<br/>
</div>
</div>
</div>
divRemainsShowAndAlertHi(){
alert('Hi button click is working but drawer does not slides')
}
toggleDiv(id){
// just hide/show logic
const plateclass = document.getElementById(id);
const changedclass = plateclass.className == 'show' ? 'hide' : 'show';
plateclass.setAttribute('class', changedclass);
}
You need to stopPropagation for the click event when clicked on the button.单击按钮时,您需要为单击事件停止传播。 Also you missed bind
in onClick here onClick={this.divRemainShowAndAlertHi.bind(this, plateInfo)}
你也错过了 onClick 这里的bind
onClick={this.divRemainShowAndAlertHi.bind(this, plateInfo)}
<div onClick={this.toggleDiv.bind(this, "divid")} >
<div >
<div className="hide" id="divid" style={{zIndex: "1"}}>
<br />
<button onClick={this.divRemainShowAndAlertHi.bind(this, plateInfo)}>click me</button>
<br/>
</div>
</div>
</div>
divRemainsShowAndAlertHi(info, e){
e.stopPropagation();
alert('Hi button click is working but drawer does not slides')
}
Also don't modify the DOM element directly, do it the React way by using state也不要直接修改 DOM 元素,通过使用 state 来做 React 的方式
state = {
visibleModel: ''
}
// In render
<div onClick={this.toggleDiv.bind(this, "divid")} >
<div >
<div className={this.state.modelState === "divid" ? "show": "hide"} id="divid" style={{zIndex: "1"}}>
<br />
<button onClick={this.divRemainShowAndAlertHi.bind(this, plateInfo)}>click me</button>
<br/>
</div>
</div>
</div>
// Function implementation
toggleDiv(id){
this.setState({visibleModel: id});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.