一、方便一点的
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical">
<mx:Button id="btn" label="Show Names" click="showNames(event)"/>
<mx:Script>
<![CDATA[
private function showNames(evt:MouseEvent):void
{
var temp:Array = new Array("George","Tim","Alex","Dean");
trace(temp.toString());
}
]]>
</mx:Script>
</mx:Application>
点击button,在控制台显示temp中的内容
二、另一种方式,通过as代码来完成添加
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
creationComplete="initApp(event);">
<mx:Button id="btn" label="Show Names"/>
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
private function initApp(evt:FlexEvent):void
{
btn.addEventListener(MouseEvent.CLICK,showNames);
btn.addEventListener(MouseEvent.CLICK,showTitles);
}
private function showNames(evt:MouseEvent):void
{
var temp:Array = new Array("George","Tim","Alex","Dean");
trace(temp.toString());
}
private function showTitles(evt:MouseEvent):void
{
var temp:Array = new Array("Director","Vice-President","President",
"CEO");
trace(temp.toString());
}
]]>
</mx:Script>
</mx:Application>
在initApp方法中为button添加事件监听器,因为 btn.addEventListener(MouseEvent.CLICK,showNames);
写在 btn.addEventListener(MouseEvent.CLICK,showTitles)的前面,所以showNames方法先执行,如果想指定方法的执行顺序,可以这样修改initApp方法
private function initApp(evt:FlexEvent):void {
btn.addEventListener(MouseEvent.CLICK,showNames,false,0);
btn.addEventListener(MouseEvent.CLICK,showTitles,false,1);
}
第四个参数值大的方法先执行,当然也可以把btn.addEventListener(MouseEvent.CLICK,showTitles)写在 btn.addEventListener(MouseEvent.CLICK,showNames)前面。
<script type="text/javascript" id="wumiiRelatedItems"> </script>