onClick 获取被点击按钮的 ID [英] onClick to get the ID of the clicked button
本文介绍了onClick 获取被点击按钮的 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何找到被点击的按钮的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.
这篇关于onClick 获取被点击按钮的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文