两个按钮点击后调用同一个函数,该如何区分是哪一个按钮?

小程序 文章 2021-03-04 23:01 451 0 全屏看文

AI助手支持GPT4.0

两个按钮点击后调用同一个函数,该如何区分是哪一个按钮?The same function is called after two buttons are clicked. How to distinguish which button is?

两个按钮点击后调用同一个函数,该如何区分是哪一个按钮?

比如两个button设置两个不同的class名称,点击任意一个之后通过控制台输出点击按钮的class

For example, two buttons set two different class names, click any one, and then output the class of the button through the console

回答:

ipeng:

在两个button按钮上分别添加data-属性,比如data-type='btn1' ,data-type='btn2' ,然后在点击事件里通过e.currentTarget.dataset.type或e.target.dataset.type取到type的值,由此可以区分是点击哪个按钮触发的点击事件。

跨商通:

<button id='1'></button>

xplee:😄自问自答

-EOF-

AI助手支持GPT4.0