我想在每次单击按钮时对按钮进行控制台日志,但如果我单击按钮内的任何子元素,则会打印子元素,而不是我想要的。我怎么确定元素的事件监听器印刷吗?
const onCrossClick = (e) => {
console.log(e.target);
};
<button
className="delete-button"
onClick={onCrossClick}
>
<i className="fas fa-times-circle fa-2x"></i>
</button>
const onCrossClick = (e) => {
console.log(e.currentTarget);
};
<button
className="delete-button"
onClick={onCrossClick}>
<i className="fas fa-times-circle fa-2x"></i>
</button>
使用closest()
const onCrossClick = (e) => {
console.log(e.target.closest("button"));
};