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.
cosa succede se si utilizza 'console.log (e.tagName);' – mgraph
Penso che sia necessario un 'href' che non sia la chiamata della funzione JavaScript. Dovrebbe funzionare se usi 'href =" # "' e usi 'onclick =" function ... 'per l'esecuzione dello script. Penso che sia qualcosa a che fare con il modo in cui un anchor tag non è un vero tag senza un vero e proprio 'href'. – andyb
Non utilizzare' e' per l'oggetto. 'e' è uno standard per l'oggetto evento – gdoron