我正在尝试重新加载 iframe,如下所示:
var frameObject = $('#myFrame');
var frameAsInDom = frameObject[0].contentWindow;
var currentLocation = frameAsInDom.location;
frameObject.attr('src', 'about:blank');
frameObject.contents().remove(); // This was a desperate statement!!
frameObject.attr('src', currentLocation).load(function(){
alert('iframe reloaded');
});
问题是 iframe 的 .load 事件根本没有被触发。我没有在加载回调中收到警报。
最佳答案
您正在混合事件的绑定(bind)和触发,
查看此代码:http://jsfiddle.net/BvQuk/1/
HTML
<iframe id="myFrame" src="http://google.com"></iframe><br/>
<input id="uxButton" type="button" value="Reload the frame" />
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
});
});
希望有帮助。
关于javascript - iframe .load 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3810332/