Given function act()
in the JavaScript and <p id="one" onclick="act()">
, <p id="two" onclick="act()">
, can I get to know the p
, which has been pressed without using onclick="act(this)"
?开发者_如何学Go Thanks
No, you can't.
You need to pass this
(or event
, which contains source information).
If you want to, you can write act.call(this)
, which will call the function in the context of the element, so that this
inside act
will refer to the element.
Although some browsers also expose the event object as a global, not all browsers do this, so you cannot rely on this behavior.
Actually, it is possible:
<button onclick="called()" id="one">click</button>
<script>
function called()
{
console.log(window.event);
}
</script>
Trawl around inside window.event
to find the targeted element, event type, and other info
精彩评论