开发者

Given a property, how to find to which object the property belongs?

开发者 https://www.devze.com 2022-12-31 02:18 出处:网络
I开发者_Python百科\'ve got a few DOM elements, say text-field input A, B, C, and all of them have the property of \'onclick\'. How do I find to which object a particular \'onclick\' belongs?You\'re pr

I开发者_Python百科've got a few DOM elements, say text-field input A, B, C, and all of them have the property of 'onclick'. How do I find to which object a particular 'onclick' belongs?


You're probably trying to find the element that triggered an event handler.

If so, use the this keyword.

EDIT

Apparently, you have an inline event handler as a string, and you're trying to find the element that the handler was attached to, from the string.

This is completely impossible.

A string is just a string. Just because your string happens to contain Javascript code, and happens to be assigned to a property on an object, doesn't make it any different from any other string; Javascript does not track parent references in objects.

Instead, you should bind an anonymous function as an event handler using jQuery.
(Or, if you really don't want to use jQuery, addEventListener / attachEvent)

0

精彩评论

暂无评论...
验证码 换一张
取 消