Window.event.srcElement and Window.event.target trigger event object Acquisition (very important)

Source: Internet
Author: User

 

To determine the element that triggered the event:

var tag = Window.event.target | | Window.event.srcElement;
if (tag.tagName.toLowerCase () = = "Input") {
Return
}

Window.event.srcElement and Window.event.target are all elements that point to the triggering event and what kind of property it is.

Srcelement is an event initialization target HTML element object reference because the event bubbles through the element hierarchy and can be processed at any level.

With the reference to the element, you can read and write the attributes of the element.

IE browser supports window.event.srcElement, while Firefox supports Window.event.target;


In JS this and window.event.srcelement:[a bit of content for reprint]

Let's look at a simple example:

<input type= "text" onblur= "alert (this.value)"/> No problem at all.

So what's not going to work?

Fuction method ()

{

alert (this.value);

}

<input type= "text" onblur= "Method ()"/> This is not possible, because method () is a function called by the response function.

So what happens in this case?

Method One:

Fuction Method (BTN)

{

alert (Btn.value);

}

<input type= "text" onblur= "method (This)"/> No problem!

Method Two:

Fuction method ()

{

alert (Window.event.srcElement.value);

}

<input type= "text" onblur= "Method ()"/> No problem! Window.event.srcElement the control that gets the trigger event

We're looking at an example of a little bit of a detour.

<script type= "Text/javascript" >
function Initevent () {
var inputs = Document.getelementsbytagname_r ("input");
for (var i = 0; i < inputs.length; i++) {
Inputs[i].onblur = onblurevent;
}
}

function Onblurevent () {
//Onblurevent is a onblur response function, not a function called by a response function
//So you can use this to get the object where the event occurred
if (This.value.length > 0) {
This.style.backgroundColor = "White";
}
else {
This.style.backgroundColor = "Red";
}
}
</script>
<body onload= "initevent ()" >
<input id= "Text1" type= "text"/>
<input id= "Text2" type= "text"/>
<input id= "Text3" type= "text"/>
</body>

As shown in the red commentary, you can use this to get input for the current trigger event.

Window.event.srcElement and Window.event.target trigger event object Acquisition (very important)

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.