How to get the ID of a clicked HyperLink in it's event handler in Javascript?
How to get the ID of a clicked HyperLink in it's event 开发者_如何转开发handler in Javascript?
Inside of the event handling function, this
is the element that was actually clicked. So accessing this.id
is what you need.
A) <a id="MyID" href="javascript:alert(this.id);">Doesn't Work</a>
B) <a id="MyID" href="#" onclick="alert(this.id);">Works</a>
Not sure how to make option A work.
update As JAAulde noted in a comment. you really shouldn't use option A.
Actually this worked! this.event.srcElement.id
For IE use event.srcElement.id. For other browsers use event.target.id.
精彩评论