JS obtains the event object and the event source object (Firefox, ie)

Source: Internet
Author: User
Tags tagname

Take notes in case you forget ~~
JS gets Event Events. Different browsers have different methods.

For example, in IE, you can use window. Event in JS functions, without adding any parameters to the functions.

You can also use prototype or jquery to encapsulate events. It is better to use the JS library, otherwise there will be troubles below.

Firefox:Parameters to be passed

 <  Head  >  
< Script >
Function Click1 (event, str1 ){
Alert (Event + Str1 );

}
< / SCRIPT>
< / Head>
< Body >
< Input type = " Button " ID = " ABC " Onclick = " Click1 (event, 'kenko '); " Value = " Click me "/ >
< / Body>

IE:No parameter required

 <  Head  >  
< Script >
Function Click1 (){
Alert (event. srcelement. value );
}
< / SCRIPT>
< / Head>
< Body >
< Input type = " Button " ID = " ABC " Onclick = " Click1 (); " Value = " Click me "/ >
< / Body>

Event. srcelement

Event. srcelement. ID

========================================================== ==========================================================

In ie, the event object has the srcelement attribute, but does not have the target attribute. In Firefox, the even object has the target attribute, but does not have the srcelement attribute ..


Solution: Use OBJ (OBJ = event. srcelement? Event. srcelement:
Event.targetexample to replace event.srcelement=event.tar get under firefox .. Pay attention to
Compatibility issues.

You can capture the objects of the current event, such as event. srcelement. tagname, to capture the activity tag name.
Note that all obtained tags are expressed in uppercase, such as "TD", "TR", and ". So you can copy some of the items you have read and try again if you don't remember them.

<SCRIPT type = "text/javasOther ">
Function tdclick (){
If (event. srcelement. tagname. tolowercase () = 'td ')
Alert ("row:" + (event. srcelement. parentnode. rowindex + 1) + "column:" + (event. srcelement. cellindex + 1 ));
}
</SCRIPT>

Event. srcelement can be seen literally with the following keywords: event, the source means: the source of the current event,

We can call various attributes like document. getelementbyid,

Some people often ask how to use event. srcelement in Firefox, which is described in detail here:

In ie, the event object has the srcelement attribute, but does not have the target attribute. In Firefox, the event object has the target attribute, but does not have the srcelement attribute. However, they play a similar role, namely:

Event. srcelement under event.tar get = IE in Firefox

Solution: Use OBJ (OBJ = event. srcelement? Event. srcelement: event.targettoken used to replace event.tar get under firefox.


In
You can directly use the window. event object to process events in IE, but in Firefox, there is no
Window. when an event object is used by a function, the event must be passed as a parameter when an event occurs. Unlike in IE, the event object is global and can be accessed everywhere.
Q. The following getevent () function is compatible with Firefox and IE. You only need to call getevent () at the beginning of the function that accesses the event object. You no longer need to use the event
Parameter transfer:CodeThe experiment has passed!

Function myfunc ()
{
VaR EVT = getevent ();
VaR element = EVT. srcelement | evt.tar get;
}

Function getevent ()
{
If (document. All)
{
Return window. event; // for IE
}
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;
}

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.