javascript 如何从 iframe 内部捕获 iframe resize 事件(iframe 和页面 - 同一域)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9484191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:49:31  来源:igfitidea点击:

How catch iframe resize event from inside the iframe (iframe and the page - same domain)

javascriptjqueryiframejavascript-events

提问by Yosef

I try to listen from inside of iframe to changes of iframe width/height with:

我尝试从 iframe 内部监听 iframe 宽度/高度的变化:

$(window).resize(function(){
    alert('1 on inside iframe');
});

or:

或者:

$(window.parent).find('iframe').resize(function(){
    alert('2 on inside iframe');
}); 

but nothing happens when iframe size changes. (I need a cross-browser solution: IE7, Chrome, Firefox, Safari.)

但是当 iframe 大小改变时什么也没有发生。(我需要一个跨浏览器的解决方案:IE7、Chrome、Firefox、Safari。)

回答by Rizwan Mumtaz

$(body).resize(function(){
    alert('on inside iframe');
});

Edit :

编辑 :

in html

在 html 中

<body onresize="myFunction()">

回答by Nick Beranek

You need to call the resize()handler from the parentdocument on the iframe:

您需要resize()从以下parent文档中调用处理程序iframe

$('iframe').resize(function() {} );