DOM事件

49 阅读1分钟

DOM事件 记录方便查询

  • target:触发事件的元素\
  • currentTarget:事件绑定的元素
  • 当事件不支持冒泡的时候,两者指的是同一个元素。
  • 当事件支持冒泡时:比如父元素和子元素都绑定click事件,当点击子元素时,父元素的事件也会触发,此时父元素事件的currentTarget指向父元素,target指向子元素。
import Button from 'antd/es/button';

const Index = () => {
  const handleClick = (event: any) => {
    const theValue = event.currentTarget.value;
    console.log('the value:', theValue); //打印的是this is the value
    const theText = event.currentTarget.textContent;
    console.log('the text: ', theText); //打印的是Test
  };

  return (
    <Button value="this is the value" onClick={handleClick}>
      Test
    </Button>
  );
};
export default Index;