代码之家  ›  专栏  ›  技术社区  ›  ZX12R

iframe.load事件未触发

  •  2
  • ZX12R  · 技术社区  · 14 年前

    我正在尝试重新加载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');
    });
    

    问题是iframe的.load事件根本没有被触发。我不明白 警觉的 在负载回叫中。

    2 回复  |  直到 14 年前
        1
  •  5
  •   xmarcos    14 年前

    你把事件的绑定和触发混为一谈,

    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
        });
    
    });
    

    希望有帮助。

        2
  •  1
  •   isherwood    4 年前

    出现JavaScript错误: currentLocation 未定义。这是因为电脑上的一个打字错误 var currenctLocation

    working demo