javascript - This keyword when function is called from hyperlink -
i have many links on web page used this:
<a href="javascript:test.test(this);">testing</a>
with this
keyword expecting html element owner of function. but, shows me window
object instead. why that? how can (with code used above) html element, owner of function?
on web page, html structure more complex. every hyperlink inside other element, if this
showing current element, easy parent element.
var test = function(){ var test = function(element){ debugger; } return { test: test } }();
<div data-iuser_key='49499'> <a href="javascript:test.test(this);">testing</a> </div>
what did expect? code run inside javascript:
, has no reference target element. default this
refers window
.
instead, use proper click handler:
<a href="#" onclick="test.test(this)">
wiki
Comments
Post a Comment