JS Get Event object code _javascript Tips

Source: Internet
Author: User
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.

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.