General Practice:
Copy Code code as follows:
<input type= "button" id= "Test" value= "point I test"/>
<script type= "Text/javascript" >
var testbtn = document.getElementById (' test ');
Testbtn.onclick = Testfun;
function Testfun (e)
{
var evt = e | | window.event;
alert (EVT);
}
</script>
Or:
Copy Code code as follows:
<input type= "button" id= "Test" value= "point I test"/>
<script type= "Text/javascript" >
var testbtn = document.getElementById (' test ');
if (Window.addeventlistener)
{
Testbtn.addeventlistener (' Click ', Testfun, false);
}
else if (window.attachevent)
{
Testbtn.attachevent (' onclick ', testfun);
}
function Testfun (e)
{
var evt = e | | window.event;
alert (EVT);
}
</script>
The value returned is "[Object Event]".
But what if this is the way it is?
Copy Code code as follows:
<input type= "button" id= "test_1" value= "dot I test" onclick= "testfun_1 ()"/>
<script type= "Text/javascript" >
function testfun_1 ()
{
How do I get here?
}
</script>
"The matter is not decided to ask Baidu, foreign affairs not to ask Google," This is not false. Search, the answer is quite a lot, but most of the similarities (may be coincidence).
Http://www.jb51.net/article/19408.htm
Http://www.cnblogs.com/cuixiping/archive/2008/04/13/1150847.html
This article (seemingly reprint) of the foolish is still quite insightful.
Copy Code code as follows:
<input type= "button" id= "test_1" value= "dot I test" onclick= "testfun_1 ()"/>
<script type= "Text/javascript" >
function testfun_1 ()
{
var evt = getEvent ();
alert (EVT);
}
function GetEvent () {
if (window.event) return window.event; It's more appropriate to use object detection here.
Func=getevent.caller;
while (Func!=null) {
var arg0=func.arguments[0];
if (arg0) {
if ((arg0.constructor==event | | arg0.constructor ==mouseevent)
|| (typeof (arg0) = = "Object" && arg0.preventdefault && arg0.stoppropagation)) {
return arg0;
}
}
Func=func.caller;
}
return null;
}
</script>
Generally speaking, this kind of embedded writing is rarely used (JS is written in the HTML tag onclick= "testfun_1 ()"), and it is not recommended to use this method, it will cause maintenance and development trouble.