In order to facilitate comparison and narration, let's start with the simplestJS + htmlCode:
< Input Type = "Button" Value = "Test" ID = "Htmbtn" Onclick = "Alert (this. ID )" >
Usage JS + html OfProgramEveryone knows, here's This Point to Html Component itself, so This. ID Display as expected "Htmbtn" .
However Flex3 When you write an event listening function, This The keyword will point Application , And Does not point to the component that triggers the event itself On ( JS + html Completely different ):
For example:
< MX: ComboBox Name = "TTT" Change = "Alert. Show ('you have change the data! '+ This. selectedlabel )" >
< MX: dataprovider >
< MX: String > Dogs </ MX: String >
< MX: String > Cats </ MX: String >
< MX: String > Mice </ MX: String >
</ MX: dataprovider >
</ MX: ComboBox >
This. selectedlabel no theme is found. Because This does not point to this ComboBox !!!
how can we find the component that triggers the event? flex provides event. currenttarget to specify the current component that triggers the event, as follows:
< MX: ComboBox Name = "TTT" Change = "Alert. Show ('you have change the data! '+ Event. currenttarget. selectedlabel )" >
< MX: dataprovider >
< MX: String > Dogs </ MX: String >
< MX: String > Cats </ MX: String >
< MX: String > Mice </ MX: String >
</ MX: dataprovider >
</ MX: ComboBox >
Or:
<MX: script><! [CDATA [
Private function changeevt (E: Event): void {
Alert. Show ('you have change the data! '+ E. currenttarget. selectedlabel )"
}
]></MX: script>
< MX: ComboBox Name = "TTT" Change = "Changeevt (event )" > < MX: dataprovider >
< MX: String > Dogs </ MX: String >
< MX: String > Cats </ MX: String >
< MX: String > Mice </ MX: String >
</ MX: dataprovider >
</ MX: ComboBox >
the official documents also provide special instructions, use event. currenttarget it is recommended that you specify the component type first, for example, textinput (E. currenttarget) to prevent running errors when a property of this component does not exist, you can see the code:
<? XML version = "1.0" ?>
<MX: ApplicationXmlns: MX= "Http://www.adobe.com/2006/mxml">
<MX: script><! [CDATA [
Import MX. Core. uicomponent;
Private function tihandler (E: Event): void {
/*
This code will report an error during running, but no error will be reported during compilation. Because textinput does not have the tmesis attribute at all
E. currenttarget. tmesis = 4;
*/
/*
The usage of this code can be found during compilation, and the error does not need to be reported during runtime.
Textinput (E. currenttarget). tmesis = 4;
*/
}
]></MX: script>
<MX: textinputID= "Ti1"Click= "Tihandler (event )"
Text= "This is some text. When you click on this control, the first three characters
Are selected ."/>
</MX: Application>