Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

javascript - JavaScript - onClick获取单击按钮的ID(JavaScript - onClick to get the ID of the clicked button)

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

(如何找到被点击的按钮的ID?)

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

function reply_click()
{
}
  ask by Bin Chen translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to send the ID as the function parameters.

(您需要将ID作为函数参数发送。)

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> 

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

(这将发送ID this.id作为clicked_id ,您可以在您的函数中使用它。)

See it in action here.

(在这里看到它。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...