我有一个有多行的表。每行都有一个 onClick 属性,该属性调用类的 handleClick 方法。每个都有多个子元素。所有子元素都没有 onClick 属性,因此它们不应触发 handleClick 方法。
这是代码:
...
export class EventRow extends Component {
...
handleClick = (e) => {
e.preventDefault();
console.log(e.target.id);
console.log(e.target.nodeName);
}
...
render () {
return (
<tr id = { "tr" + this.props.eventdefinition.event_id } className = "noselect" onClick = { this.handleClick.bind(this) }>
<td className = "noselect">{this.props.eventdefinition.event_name}</td>
<td>
<div className = "custom-control custom-checkbox tdDiv noselect">
<input type="checkbox" className = "custom-control-input" id = { "customCheck" + this.props.eventdefinition.event_id } readOnly checked = {this.props.eventdefinition.recurring} />
<label className = "custom-control-label" htmlFor = {"customCheck" + this.props.eventdefinition.event_id}></label>
</div>
</td>
<td>
<div className = "custom-control custom-switch tdDiv noselect">
<input type="checkbox" className = "custom-control-input" id = { "customSwitch" + this.props.eventdefinition.event_id } checked = {this.props.eventdefinition.active_for_generation} onChange = { this.onChange }/>
<label className = "custom-control-label" htmlFor = {"customSwitch" + this.props.eventdefinition.event_id}></label>
</div>
</td>
</tr>
);
}
}
...
现在您可以看到在handleClick 方法中,有两个console.log 语句。当我点击我的行时,我希望在我的控制台中我会得到一个 id 和tr3一个节点名称TR,但是我得到的是也被点击的任何子项的 id(通常是黑色) ,以及任何子项的节点名称也被点击TD或INPUT或LABEL。我的事件触发器发生了什么?
慕森卡
ABOUTYOU
相关分类