SoFunction
Updated on 2025-04-07

Js get event object code

General practice:
Copy the codeThe code is as follows:

<input type="button" value="click me to test" />
<script type="text/javascript">
var testBtn = ('test');
= testFun;
function testFun(e)
{
var evt = e || ;
alert(evt);
}
</script>

or:
Copy the codeThe code is as follows:

<input type="button" value="click me to test" />
<script type="text/javascript">
var testBtn = ('test');
if()
{
('click', testFun, false);
}
else if()
{
('onclick', testFun);
}
function testFun(e)
{
var evt = e || ;
alert(evt);
}
</script>

The returned values ​​are "[object Event]".
But what if this is the way?
Copy the codeThe code is as follows:

<input type="button" value="click me to test" onclick="testFun_1()" />
<script type="text/javascript">
function testFun_1()
{
//How to get it here?
}
</script>

"If you don't decide on internal affairs, ask Baidu, and if you don't decide on external affairs, ask Google." This is true. After searching, there are quite a lot of answers, but most of them are similar (maybe coincidental).
https:///article/
/cuixiping/archive/2008/04/13/
Yugong's article (seemingly reposted) is quite insightful.
Copy the codeThe code is as follows:

<input type="button" value="click me to test" onclick="testFun_1()" />
<script type="text/javascript">
function testFun_1()
{
var evt = getEvent();
alert(evt);
}
function getEvent(){
if() return ; //It is more appropriate to use object detection here
func=;
while(func!=null){
var arg0=[0];
if(arg0){
if((==Event || ==MouseEvent)
|| (typeof(arg0)=="object" && && )){
return arg0;
}
}
func=;
}
return null;
}
</script>

Generally speaking, this embedded writing method is rarely used (js is written onclick="testFun_1()" in the html tag), and it is not recommended to use this method, which will cause maintenance and development troubles.