iframe不会触发调整大小事件

erm*_*mSO 13 javascript iframe jquery resize event-handling

经过3天的研究和反复试验,我无法获取iframe及其内容来触发resize事件,以便调用我的resize函数.如果我使用...触发器("调整大小"); 要手动触发resize事件,我的调整大小函数被调用并起作用.iframe中加载http://localhost:81/curlExample/的页面与包含iframe的页面位于同一个域()上.最终,iframe中的页面将由php curl方法提供,但我想先让它工作.

*******更新********当我调整浏览器窗口大小导致iframe调整其大小时,如何触发调整大小事件?


谢谢您的帮助!

带有iframe的页面

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   
    function setResize()
    {
        window.alert("Hello");
      
        var iframeRef = document.getElementById('displayframe');
        $(iframeRef).on("resize", function(){
            var xExp = 0;
            window.alert("Resize event fired.");
            
        });
    }
  
    $('#displayframe').load(function()
    {
        alert("Hello from iFrame.  Load event fired.");
        var myStallTime = setTimeout(setResize, 3000);

    });

});
</script>
</head>
<body>

<p id="myP">Hello</p>

<iframe id="displayframe" src="http://localhost:81/curlExample/HelloIframe.xhtml" style="height:250px; width:100%;">
  <p>Your browser does not support iframes.</p>
</iframe>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

iframe中的页面(HelloIframe.xhtml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>TODO supply a title</title>
    </head>
    <body>
        <div id="myContent" style="width:100%; height:200px;">
            <h1>TODO write content</h1>
            <h2>Extremity sweetness difficult behaviour he of</h2>

            <p>Agreed joy vanity regret met may ladies oppose who. Mile fail as left as hard eyes. Meet made call in mean four year it to. Prospect so branched wondered sensible of up. For gay consisted resolving pronounce sportsman saw discovery not. Northward or household as conveying we earnestly believing. No in up contrasted discretion inhabiting excellence. Entreaties we collecting unpleasant at everything conviction.</p>

            <p>Yet remarkably appearance get him his projection. Diverted endeavor bed peculiar men the not desirous. Acuteness abilities ask can offending furnished fulfilled sex. Warrant fifteen exposed ye at mistake. Blush since so in noisy still built up an again. As young ye hopes no he place means. Partiality diminution gay yet entreaties admiration. In mr it he mention perhaps attempt pointed suppose. Unknown ye chamber of warrant of norland arrived.</p>

        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Aur*_*ium 21

<iframe>元素永远不会触发resize事件,如a <img>或a <div>.你必须得到这个事件window.由于您的iframe文档来自与父文档相同的来源,因此您可以访问其文档contentWindow和任何其他属性.

所以,试试这个:

var iframeWin = document.getElementById('displayframe').contentWindow;
$(iframeWin).on('resize', function(){ ... });
Run Code Online (Sandbox Code Playgroud)

我只用DOM做过addEventListener.

var iframeWin = document.getElementById('displayframe').contentWindow;
iframeWin.addEventListener('resize', function(){ ... });
Run Code Online (Sandbox Code Playgroud)

  • 一年后,Resize Observer仍然只在一个浏览器上支持,而不是100%polyfilled (4认同)
  • 2年后,我们现在在浏览器中安装了Resize Observer,可以让您观察各个元素的调整大小事件https://developers.google.com/web/updates/2016/10/resizeobserver (3认同)