简体   繁体   English

锚标记点击以区分相同的 onClick Javascript 函数

[英]Anchor tag clicks to differentiate within same onClick Javascript function

I have a simple anchor tag as below:我有一个简单的锚标签,如下所示:

<a onclick="{!c.handleClick}" >Major</a>
<a onclick="{!c.handleClick}" >Minor</a>

Within handleClick function, I am performing DML operations that are common for both urls.在 handleClick 函数中,我正在执行两个 url 通用的 DML 操作。 Then I am trying to differentiate Major/Minor clicks and perform necessary actions and then redirect to respective file.然后我试图区分主要/次要点击并执行必要的操作,然后重定向到相应的文件。 May I know what element I can add to the tag and make the clicks differentiable?我可以知道我可以向标签添加什么元素并使点击可区分吗?

You could add an ID to each anchor and then target based off that:您可以为每个锚点添加一个ID ,然后以此为基础定位:

 function handleClick(e) { e.preventDefault(); const id = e.target.id; if (id === 'major') { console.log('Major clicked'); } else { console.log('Minor clicked'); } }
 <a href="#" onclick="handleClick(event)" id="major">Major</a> <a href="#" onclick="handleClick(event)" id="minor">Minor</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM