javascript – 通过单击子div来防止onClick事件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 通过单击子div来防止onClick事件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4015字,纯文字阅读大概需要6分钟。
内容图文
![javascript – 通过单击子div来防止onClick事件](/upload/InfoBanner/zyjiaocheng/697/f8198a0243ae4804b60a8ea9f7553231.jpg)
我正在尝试在React JS中创建一个模态
我有一个外部div,这是整个身体,我有我内在的div.我想应用函数来关闭模式,如果它被点击在内部div之外.
我的代码如下:
popupOutterDivStyle() {
return {
zIndex: 10000,
position: "fixed",
width: "100%",
height: "100%",
top: 0,
left: 0,
background: "rgba(102,102,102,0.8)"
}
}
popupInnerDivStyle() {
return {
zIndex: 20000,
position: "fixed",
width: "70%",
top: "50%",
left: "50%",
height: "400px",
marginTop: "-200px", /*set to a negative number 1/2 of your height*/
marginLeft: "-35%", /*set to a negative number 1/2 of your width*/
background: "rgba(255,255,255,1)",
display: 'block'
}
}
closePopupIcon() {
return {
position: "absolute",
right: -25,
top: - 27,
zIndex: 30000,
cursor: "pointer"
}
}
render() {
const animationSettings = {
transitionName: "example",
transitionEnterTimeout: 500,
transitionAppearTimeout: 500,
transitionLeaveTimeout: 500,
transitionAppear: true,
transitionLeave: true
};
return (
<div onClick={this.props.closeModal}>
<ReactCSSTransitionGroup {...animationSettings}>
<div key={this.props.modalState} style={this.popupOutterDivStyle()} className={showModal}>
<div style={this.popupInnerDivStyle()}>
<a href="#" style={this.closePopupIcon()} onClick={this.props.closeModal}><i className="closePopup ion-ios-close-empty" /></a>
{this.props.children}
</div>
</div>
</ReactCSSTransitionGroup>
</div>
);
}
当我点击带有图标的链接或当我点击内部div之外时,它工作正常.
但问题是,如果我点击内部div,它也会关闭.
我不想使用jquery.
有什么建议?
UPDATE
stopPropagation(event){
event.stopPropagation();
}
<div>
<ReactCSSTransitionGroup {...animationSettings}>
<div id="outter" key={this.props.modalState} style={this.popupOutterDivStyle()} className={showModal} onClick={this.props.closeModal}>
<div id="inner" style={this.popupInnerDivStyle()} onClick={this.stopPropagation.bind(this)}>
<a href="#" style={this.closePopupIcon()} onClick={this.props.closeModal}><i className="closePopup ion-ios-close-empty" /></a>
{this.props.children}
</div>
</div>
</ReactCSSTransitionGroup>
</div>
在我的案例中this.props.children是一个联系表格:
export default class ContactForm extends React.Component {
constructor(props) {
super(props);
this.state = {
senderName: '',
email: '',
message: '',
errors: {}
};
this.sendingHandle = this.sendingHandle.bind(this);
this.contactHandle = this.contactHandle.bind(this);
}
contactHandle(event) {
let field = event.target.name;
let value = event.target.value;
console.log(field);
}
sendingHandle(event) {
event.preventDefault();
}
render() {
const language = this.props.currentLanguage.homePage;
return (
<div className="contact-form">
<form>
<div className="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<TextInput
type="text"
name="senderName"
label={language.contactNameLabel}
labelClass="contactLabel"
placeholder={language.contactNameLabel}
className="templateInput"
icon="user"
iconSize="15x"
iconClass="contactFaIcon"
onChange={this.contactHandle}
value={this.state.name}
errors={this.state.errors.senderName}
/>
</div>
<div className="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<TextInput
type="text"
name="email"
label={language.contactEmailLabel}
labelClass="contactLabel"
placeholder={language.contactEmailLabel}
className="templateInput"
icon="envelope-o"
iconSize="15x"
iconClass="contactFaIcon"
onChange={this.contactHandle}
value={this.state.email}
errors={this.state.errors.email}
/>
</div>
<div className="col-md-12 col-sm-12 col-xs-12">
<Textarea
className="message"
name="message"
placeholder={language.contactMessageLabel}
label={language.contactMessageLabel}
labelClass="contactLabel"
icon="comments-o"
iconSize="15x"
iconClass="contactFaIcon"
onChange={this.contactHandle}
errors={this.state.errors.message}
/>
</div>
<div className="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">
<Button text="Verzenden" handleClick={this.sendingHandle.bind(this)}/>
</div>
</form>
<div className="clearfix" />
</div>
);
}
}
解决方法:
将函数附加到内部div以停止传播.
function stopPropagation(e) {
e.stopPropagation();
}
在您的情况下< div style = {this.popupInnerDivStyle()} onClick = {stopPropagation}>
这对你有帮助:ReactJS SyntheticEvent stopPropagation() only works with React events?
内容总结
以上是互联网集市为您收集整理的javascript – 通过单击子div来防止onClick事件全部内容,希望文章能够帮你解决javascript – 通过单击子div来防止onClick事件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。