iframe .load事件未触发

ZX1*_*12R 2 html javascript jquery

我正在尝试重新加载iframe,如下所示:

var frameObject = $('#myFrame');
var frameAsInDom = frameObject[0].contentWindow;
var currenctLocation = frameAsInDom.location;
frameObject.attr('src', 'about:blank');
frameObject.contents().remove(); // This was a desperate statement!!
frameObject.attr('src', currentLocation).load(function(){
   alert('iframe reloaded');
});
Run Code Online (Sandbox Code Playgroud)

问题是iframe的.load事件根本没有被触发.我没有在负载回调中收到警报.

xma*_*cos 5

您正在混合绑定和触发事件,

看到这个代码:http://jsfiddle.net/BvQuk/1/

HTML

<iframe id="myFrame" src="http://google.com"></iframe><br/>
<input id="uxButton" type="button" value="Reload the frame" />
Run Code Online (Sandbox Code Playgroud)

JS

$(document).ready(function() {
    var iFrame = $('#myFrame');

    iFrame.bind('load', function() { //binds the event
        alert('iFrame Reloaded');
    });

    $('#uxButton').click(function() {
        alert('onButtonClick OR onBeforeReload');
        var newSrc = iFrame.attr('src') + '?c=' + Math.random(); //force new URL
        iFrame.attr('src', newSrc); //changing the src triggers the load event
    });

});
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.