正如您在标题中所说,我在使用AS3 ExternalInterface和jQuery / swfobject时遇到了一些问题。
ActionScript:
if (ExternalInterface.available) {
ExternalInterface.call('init');
ExternalInterface.addCallback('testFunc', returnFunc)
}
function returnFunc():void {
ExternalInterface.call('alertFunc');
}
jQuery的:
function init() {
alert('init');
$('#swf_object').testFunc();
}
function alertFunc() {
alert('finished');
}
显然,这意味着该对象具有id'swf_object'
我也尝试通过以下方式获取对象:
document.getElementById('swf_object')
document.getElementById('swf_object')[0]
$('#swf_object')[0]
无济于事。
它正在发出第一个警报('init'),但之后没有做最后一个警报。我完全不知所措,希望有人能指出我的错误! (在某处肯定会有一个非常明显的)
答案 0 :(得分:2)
问题在于,您在调用Flash init()
之前调用了JavaScript testFunc()
,然后才能使其成为可用的(仅发生 之后init()
的呼叫完成。
要解决此问题,只需将两行换成:
ExternalInterface.addCallback('testFunc', returnFunc); // Needs to be available before it's used
ExternalInterface.call('init');
至于在JavaScript中获取Flash对象,可以直接使用document.getElementById('swf_object')
来完成,但也可以使用jQuery:
var swf = $('#swf_object').get(0); // Get the actual object without the jQuery wrapper