reactjs - 单击子元素时如何停止父元素上的单击事件
问题描述
我在 div 中有一个按钮。在他们的点击事件中,我正在执行两种不同的功能。要求是当子元素被点击时,父元素的onClick事件不应该触发。
redirectCTD()
{
console.log("inside Parent Element");
//other stuffs to do
}
openTileModal(toolName)
{
console.log("inside Child Element");
this.setState({
isOpenTile: true,
toolSelected: toolName
});
}
<div className="col-md-12 tool ctd_tool" onClick={this.redirectCTD}>
<button onClick={this.openTileModal.bind(this,"ctd")}><img src={require("../../../images/icon-expand-blue.png")} /></button>
</div>
我曾尝试使用event.stopPropogation
and event.preventDefault
insideopenTileModal()
方法。
openTileModal(toolName,event)
{
event.stopPropogation();
console.log("inside Child Element");
this.setState({
isOpenTile: true,
toolSelected: toolName
});
}
但这不起作用,我在控制台中遇到错误,“ e.stopPropogation is not a function
”。有人可以帮助我实现目标。提前致谢
解决方案
你拼写Propagation
错误。尝试这个:
openTileModal(toolName,event)
{
event.stopPropagation();
console.log("inside Child Element");
this.setState({
isOpenTile: true,
toolSelected: toolName
});
}
推荐阅读
- image - 如何更改更改windows 10登录背景图片
- google-maps - 谷歌地图上多个动态位置的信息窗口未正确显示
- python - 用点替换字符串中的交替逗号
- python - 迷宫里的小偷
- ruby-on-rails - 获取给定 ActiveSupport::TimeWithZone 实例的完整时区名称
- git - 如何在本地和远程永久删除 git 中的提交?
- ios - 控制被建模数据的身份意味着什么?
- jquery - 将 html 表转换为 Json 数据并将数据传递给 jquery 中的 ajax 调用
- cordova - deviceReady 事件未触发
- node.js - 如何将 knex 迁移打包到模块中?