General Practice:
Copy codeThe Code is as follows:
<Input type = "button" id = "test" value = "click my 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 codeThe Code is as follows:
<Input type = "button" id = "test" value = "click my 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 returned values are "[object Event]".
But what if this method is used?
Copy codeThe Code is as follows:
<Input type = "button" id = "test_1" value = "Point me test" onclick = "testFun_1 ()"/>
<Script type = "text/javascript">
Function testFun_1 ()
{
// How do I obtain it?
}
</Script>
"You cannot decide to ask Baidu or Google for foreign affairs. After searching, there are a lot of answers, but most of them are the same (probably a coincidence ).
Http://www.jb51.net/article/19408.htm
Http://www.cnblogs.com/cuixiping/archive/2008/04/13/1150847.html
Yugong's article (which seems to be a post) is still insightful.
Copy codeThe Code is as follows:
<Input type = "button" id = "test_1" value = "Point me 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 is 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, this embedded Writing Method (onclick = "testFun_1 ()" in html tags written in js) is rarely used, and this method is not recommended, which may cause Maintenance and Development troubles.