在Flex(Flash)中嵌入HTML代碼或頁面(Flex IFrame)
在flex組件中嵌入html代碼,可以利用flex iframe。這個在很多時候會用到的,有時候flex必須得這樣做,如果你不這樣做還真不行……
flex而且可以和html進行JavaScript交互操作,flex調(diào)用到html中的JavaScript方法以及獲取調(diào)用后的返回值。
1、flex iframe下載地址:https://github.com/downloads/flex-users/flex-iframe/flex-iframe-1.5.1.zip
下載完成后目錄如下
asdoc就是文檔doc了
bin有需要用到的flex庫 swc
examples就是示例
sources源代碼
2、將bin目錄中的swc引入到你的flex工程中,并加入代碼片段如下
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:flexiframe="http://code.google.com/p/flex-iframe/"
horizontalAlign="center" verticalAlign="middle" xmlns:s="library://ns.adobe.com/flex/spark">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
protected function sayHelloHandler(event:MouseEvent):void {
// 調(diào)用當前iframe嵌入頁面中的sayHello 的JavaScript方法
iFrameBySource.callIFrameFunction("sayHello");
}
protected function sayHandler(event:MouseEvent):void {
// 調(diào)用當前iframe嵌入頁面中的say的JavaScript方法,并傳入一個參數(shù)
iFrameBySource.callIFrameFunction("say", ["hello world!"]);
}
protected function sayHiHandler(event:MouseEvent):void {
// 調(diào)用當前iframe嵌入頁面中的sayHi的JavaScript方法,并傳入2個參數(shù)。sayHi方法會返回一個字符串,最后一個回調(diào)就是輸出值的函數(shù)
iFrameBySource.callIFrameFunction("sayHi", ["hello world", "李四"], function (data:*): void {
Alert.show(data);
});
}
]]>
</mx:Script>
<!-- HTML content stored in a String -->
<mx:String id="iFrameHTMLContent">
<![CDATA[
<html>
<head>
<title>About</title>
</head>
<body>
<div>About</div>
<p>Simple HTML Test application. This test app loads a page of html locally.</p>
<div>Credits</div>
<p> </p>
<p>IFrame.as is based on the work of</p>
<ul>
<li><a target="_top">Christophe Coenraets</a></li>
<li><a target="_top">Brian Deitte</a></li>
</ul>
</body>
</html>
]]>
</mx:String>
<mx:Panel width="80%" height="80%" title="使用source本地遠程頁面">
<flexiframe:IFrame id="iFrameBySource" width="100%" height="100%" source="frame.html"/>
<s:Button label="sayHello" click="sayHelloHandler(event)"/>
<s:Button label="say-param" click="sayHandler(event)"/>
<s:Button label="sayHi" click="sayHiHandler(event)"/>
</mx:Panel>
<mx:Panel width="80%" height="80%" title="使用source加載遠程頁面">
<flexiframe:IFrame id="iFrameByRemoteSource" width="100%" height="100%" source="http://www.baidu.com" visible="true"
overlayDetection="true" />
</mx:Panel>
<mx:Panel width="80%" height="80%" title="使用content屬性 加載本地html文本內(nèi)容">
<flexiframe:IFrame id="iFrameByContent" width="100%" height="100%" content="{iFrameHTMLContent}"/>
</mx:Panel>
</mx:Application>
frame.html 頁面內(nèi)容
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>frame.html</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script type="text/javascript">
// 無參數(shù)
function sayHello() {
alert("hello......");
}
// 1個參數(shù)
function say(message) {
alert("your say: " + message);
}
// 多個參數(shù) 并返回值
function sayHi(message, name) {
alert("your say: " + message + ", name: " + name);
return "your say: " + message + ", name: " + name;
}
</script>
</head>
<body>
flex frame example html page!
<input type="button" value="say" onclick="sayHello()"/>
</body>
</html>
要注意的是:你的flex項目工程需要發(fā)表到http的應用服務器(如tomcat、jboss、iis)這些服務器中,用http請求方式才能調(diào)用到頁面內(nèi)容和JavaScript方法。如果不發(fā)布到應用服務器中,那樣只能在iframe中嵌套遠程的http請求的頁面,本地靜態(tài)頁面是無法顯示的。
相關文章
Flex 基于數(shù)據(jù)源的Menu Tree實現(xiàn)代碼
由外部參數(shù)flashvars指定數(shù)據(jù)源的文件位置或render鏈接,在源數(shù)據(jù)上加href和target屬性來控制打開窗口,可自定義父節(jié)點和子節(jié)點圖標,不設置采用系統(tǒng)默認,感興趣的你可以了解下啊,或許對你有所幫助2013-01-01Flex中對表格中某列的值進行數(shù)字格式化保留兩位小數(shù)
表格中展示的比率,對比率的處理是:保留兩位小數(shù),并向上保留。通過對某列的值進行數(shù)字格式化來實現(xiàn)保留兩位小數(shù)2014-10-10Flex調(diào)Javascript打開新窗口示例代碼
Flex通過調(diào)用Javascript打開全屏的新窗口新窗口示例代碼 ,具體實現(xiàn)代碼如下,感興趣的朋友可以參考下,希望對大家有所幫助2013-08-08Flex4 DataGrid中嵌入RadioButton實現(xiàn)思路及代碼
本節(jié)主要介紹了Flex4 DataGrid中嵌入RadioButton實現(xiàn)思路及代碼,需要的朋友可以參考下2014-07-07flex實現(xiàn)DataGrid高亮顯示數(shù)據(jù)功能的解決方案
原生的DataGrid根本無法達到所要的效果,目前一般就是來改寫原生的DataGrid,只需重新寫一個類來重寫drawRowBackground方法就可以了2013-10-10