I expect this to print "a" because when I call foo(this)
, the argument seems to be the link tag.<a href="javascript:foo(this)"> passes Window, I want the tag element itself
<script type="text/javascript">
function foo (e) {
alert (e .tagName);
}
</script>
<a href="javascript:foo(this)">click</a>
Instead, it prints "undefined". If I alert(e)
it says "object Window". How do I make foo
know which element launched it? Without passing/looking up ids.
co, jeśli użyjesz 'console.log (e.tagName);' – mgraph
Myślę, że potrzebujesz 'href', który nie jest wywołaniem funkcji JavaScript. Powinno działać, jeśli użyjesz 'href =" # "' i użyjesz funkcji 'onclick =" ... 'do wykonania skryptu. Myślę, że to ma coś wspólnego z tym, że znacznik zakotwiczenia nie jest prawdziwym tagiem bez właściwego 'href'. – andyb
Nie używaj' e' dla obiektu obj. 'e' jest standardem dla obiektu zdarzenia – gdoron