Js get event object code

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.