我正在使用Flex builder 4.5,我的问题是这个代码不起作用,当我使用trace(event.target)时,我在控制台中得到了结果,
deleteme.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.VGroup5.button1
如果我在'if'语句代码中替换这条长行。(deleteme是项目名称)。你不认为它应该只说button1而不是所有层次结构的所有长线,如果是这样的情况那么我们如何缩短它?
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
initialize="handleClick(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private function init():void{
button2.addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(event:Event):void{
trace(event.target);
if(event.target == "button1"){
button1.label = "Button 1 clicked";
}else if(event.target == "button2"){
button2.label = "Button 2 clicked";
}
}
]]>
</fx:Script>
<s:VGroup width="100%">
<s:Button id="button1" label="Button 1" click="handleClick(event)"/>
<s:Button id="button2" label="Button 2" />
</s:VGroup>
</s:Application>
提前感谢,
(我尝试使用sdk 4.1仍然是相同的答案)
答案 0 :(得分:2)
请改用以下代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
initialize="handleClick(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private function init():void{
button2.addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(event:Event):void{
trace(event.target);
if(event.currentTarget == button1){
button1.label = "Button 1 clicked";
}else if(event.currentTarget == button2){
button2.label = "Button 2 clicked";
}
}
]]>
</fx:Script>
<s:VGroup width="100%">
<s:Button id="button1" label="Button 1" click="handleClick(event)"/>
<s:Button id="button2" label="Button 2" />
</s:VGroup>
</s:Application>
将视觉对象与字符串进行比较是没有意义的。将对象与对象本身进行比较。