JavaScript - onClick获取单击按钮的ID [英] JavaScript - onClick to get the ID of the clicked button

查看:837
本文介绍了JavaScript - onClick获取单击按钮的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



How do find the id of the button which is being clicked?

<button id="1" onClick="reply_click()"></button>
<button id="2" onClick="reply_click()"></button>
<button id="3" onClick="reply_click()"></button>

function reply_click()
{
}


推荐答案

您需要发送ID作为函数参数。这样做:

You need to send the ID as the function parameters. Do it like this:

<button id="1" onClick="reply_click(this.id)">B1</button>
<button id="2" onClick="reply_click(this.id)">B2</button>
<button id="3" onClick="reply_click(this.id)">B3</button>

<script type="text/javascript">
function reply_click(clicked_id)
{
    alert(clicked_id);
}
</script>

这会将ID this.id 作为 clicked_id ,你可以在你的函数中使用它。 请在此处查看。

This will send the ID this.id as clicked_id which you can use in your function. See it in action here.

这篇关于JavaScript - onClick获取单击按钮的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆