我有一個有多行的表。每行都有一個 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。我的事件觸發器發生了什么?
為什么子元素屬性會傳遞到父 <tr> 元素的 onClick 事件(React)中?
哆啦的時光機
2023-08-24 18:02:01