从多个链接<a></a>触发的htmlloader对象中运行AS3函数

时间:2013-03-19 18:33:54

标签: actionscript-3 air

使用此代码,我可以在我的HtmlLoader中加载的内容链接上的点击事件中运行ONE AS3功能:

加载html的内容:

<html> 
<body> 
    <a href="#" id="testLink">Click me.</a> 
</html>

AS3

var html:HTMLLoader = new HTMLLoader( ); 
var urlReq:URLRequest = new URLRequest("test.html"); 
html.load(urlReq); 
html.addEventListener(Event.COMPLETE, completeHandler); 

function completeHandler(event:Event):void { 
    html.window.document.getElementById("testLink").onclick = clickHandler; 
} 

function clickHandler( event:Object ):void { 
    trace("Event of type: " + event.type ); 
}

现在的问题是,我想从html内容中将参数传递给clickHandler函数,而且我想要有几个链接:

<a class="fireAS3" href="someData1">bla bla bla</a>
<a class="fireAS3" href="someData2">bla bla bla</a>
<a class="fireAS3" href="someData3">bla bla bla</a>

所以使用Ids不是一个选项,我想我需要类但是使用getElementByIdClassName会给我一个错误(函数不存在)。那么,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您好您可以使用LocationChangeEvent进行JS-&gt; AS3“对话”,这样您就可以通过这种方式准备链接,从而提供使用JSON传递的大量参数。 Folloowing是我的程序的摘录,它在StageWebViewHTMLLoader中具有相同的事件)中呈现长列表,并获取有关它的一些详细信息的点击事件:

webView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, onWebLocationUpdate, false, 0, true);
webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, onWebLocationUpdate, false, 0, true);

protected function onWebLocationUpdate(e:LocationChangeEvent):void
{
   trace(e);
    //JS->AS
    if (e.location.indexOf("message://") != -1)
    {
        var jsonString:String = e.location.substr("message://".length);
        var myObject:Object = JSON.parse( jsonString );
        for (var name:String in myObject)
        {
            trace(name + ":" + myObject[name]);
        }
        e.preventDefault();
    }
}

JS

jQuery(document).ready(function() {
    // do something here
    //alert("Ready");
    $(".rowItem").click(function(e)
    {
        $(this).toggleClass("rowItemSelected");
        $(this).bridgeSend($(this).attr("id"),$(this).hasClass("rowItemSelected"));
    });
});

(function( $ ){
    $.fn.bridgeSend = function(id, selected)
    {
        var method = "select";
        var json = "{\"item\":{\"selected\":"+selected+",\"name\":\""+id+"\"},\"method\":\""+method+"\"}";
        window.location = "message://"+json;
    };
})( jQuery );

HTML

    <div class="rowItem" id="entry2">
        <div class="top">
            <div class="icon"><img src="css/clock.png" /></div>
            <div class="date"><h3>Fri, 04-09-2010</h3></div>
        </div><!-- end of the top -->
        <div class="desc">
            <p><strong>Title</strong></p>
            <p>The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
        </div><!-- end of the desc -->
        <div class="times">
            <div class="col1">09:00</div>
            <div class="col2">09:30</div>
            <div class="col3">0.50</div>
        </div>
    </div><!-- end of the rowItem -->

答案 1 :(得分:0)

为了便于air和js之间的通信,我想你可以轻松使用我们编码的RichWebView ANE https://github.com/myflashlab/webView-ANE