如何将触发`onclick`事件的元素的id传递给事件处理函数 [英] How to pass the id of an element that triggers an `onclick` event to the event handling function
本文介绍了如何将触发`onclick`事件的元素的id传递给事件处理函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将触发 onclick
事件的元素的id传递给事件处理函数。
How do I pass the id of an element that triggers an onclick
event to the event handling function.
我正在做这样的事情 -
I am doing something like this-
<link onclick="doWithThisElement(id_of_this_element)" />
推荐答案
不是传递ID,而是传递元素本身:
Instead of passing the ID, you can just pass the element itself:
<link onclick="doWithThisElement(this)" />
或者,如果您坚持传递ID:
Or, if you insist on passing the ID:
<link id="foo" onclick="doWithThisElement(this.id)" />
这里是JSFiddle演示: http://jsfiddle.net/dRkuv/
Here's the JSFiddle Demo: http://jsfiddle.net/dRkuv/
这篇关于如何将触发`onclick`事件的元素的id传递给事件处理函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文