Javascript 按下JS按钮并调整iframe的大小

Javascript 按下JS按钮并调整iframe的大小,javascript,jquery,html,ajax,iframe,Javascript,Jquery,Html,Ajax,Iframe,当我按下iframe中的按钮时,它会消失标记,但不会调整iframe的大小。完成ajax后,应在另一个页面中调用AjustResizeIframeHeight方法 有什么建议吗 更新 带iframe的页面 <script language="javascript" type="text/javascript"> function resizeIframe(obj) { obj.style.height = obj.contentWindow.document.body.scroll

当我按下iframe中的按钮时,它会消失标记,但不会调整iframe的大小。完成ajax后,应在另一个页面中调用AjustResizeIframeHeight方法 有什么建议吗

更新

带iframe的页面

<script language="javascript" type="text/javascript">
function resizeIframe(obj) {
 obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
function AdjustIframeHeight(i) { document.getElementById("form-iframe").style.height = parseInt(i) + "px"; }
</script>

<iframe id"form-iframe" width="100%" style="border: none" src="{{ notes }}"  onload='javascript:resizeIframe(this);'>
</iframe>
<script language="javascript" type="text/javascript">
function resizeIframe(obj) {
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
function AdjustIframeHeight(i)     {parent.AdjustIframeHeight(document.getElementById("page").scrollHeight);
</script>

<iframe id"form-iframe" width="100%" style="border: none" src="{{ notes }}"  onload='javascript:resizeIframe(this);'>
</iframe>

功能调整框架(obj){
obj.style.height=obj.contentWindow.document.body.scrollHeight+'px';
}
函数AdjustIframeHeight(i){document.getElementById(“form iframe”).style.height=parseInt(i)+“px”}
iframe的内容:

<a href="#" onclick="xpto2(1,{{ n.id }});parentNode.parentNode.removeChild(parentNode)" >Don't show again</a>

<script>
    function xpto2(status,noteid) {
       var request =  $.ajax({
           ....
       });
        request.done(function() {
           window.parent.AdjustIframeHeight();
        });
}
</script>
<div id="page"> 
<a href="#" onclick="xpto2(1,{{ n.id }});parentNode.parentNode.removeChild(parentNode)" >Don't show again</a>
<\div>
<script>
function xpto2(status,noteid) {
      ....
   });
    request.done(function() {
       request.done(function() {
            parent.AdjustIframeHeight(document.getElementById("page").scrollHeight);
    });
}
</script>

函数xpto2(状态,noteid){
var请求=$.ajax({
....
});
request.done(函数(){
window.parent.adjustiframehight();
});
}
在ajax函数中添加了
window.parent.AdjustIframeHeight()
,但它表示“未解析的函数或方法”

  • 当AJAX调用完成时,它什么也不做,因为您有一个空函数
  • getElementById(“form-iframe”)
    将找不到任何内容,因为没有id为
    form-iframe
    的元素
  • 如果iframe中的页面具有相同的主机、协议和端口,则应能够通过
    window.parent.AdjustIframeHeight()
    访问该功能
    如果主机、协议或端口不匹配,则必须从iframe使用,并侦听父页面上的
    消息
    事件
  • 已解决 解决方案是添加一个div,然后将该div调用到该方法

    带iframe的页面

    <script language="javascript" type="text/javascript">
    function resizeIframe(obj) {
     obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
    }
    function AdjustIframeHeight(i) { document.getElementById("form-iframe").style.height = parseInt(i) + "px"; }
    </script>
    
    <iframe id"form-iframe" width="100%" style="border: none" src="{{ notes }}"  onload='javascript:resizeIframe(this);'>
    </iframe>
    
    <script language="javascript" type="text/javascript">
    function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
    }
    function AdjustIframeHeight(i)     {parent.AdjustIframeHeight(document.getElementById("page").scrollHeight);
    </script>
    
    <iframe id"form-iframe" width="100%" style="border: none" src="{{ notes }}"  onload='javascript:resizeIframe(this);'>
    </iframe>
    
    
    功能调整框架(obj){
    obj.style.height=obj.contentWindow.document.body.scrollHeight+'px';
    }
    函数AdjustIframeHeight(i){parent.AdjustIframeHeight(document.getElementById(“page”).scrollHeight);
    
    iframe的内容:

    <a href="#" onclick="xpto2(1,{{ n.id }});parentNode.parentNode.removeChild(parentNode)" >Don't show again</a>
    
    <script>
        function xpto2(status,noteid) {
           var request =  $.ajax({
               ....
           });
            request.done(function() {
               window.parent.AdjustIframeHeight();
            });
    }
    </script>
    
    <div id="page"> 
    <a href="#" onclick="xpto2(1,{{ n.id }});parentNode.parentNode.removeChild(parentNode)" >Don't show again</a>
    <\div>
    <script>
    function xpto2(status,noteid) {
          ....
       });
        request.done(function() {
           request.done(function() {
                parent.AdjustIframeHeight(document.getElementById("page").scrollHeight);
        });
    }
    </script>
    
    
    函数xpto2(状态,noteid){
    ....
    });
    request.done(函数(){
    request.done(函数(){
    AdjustIframeHeight(document.getElementById(“page”).scrollHeight);
    });
    }
    
    我遵循您的建议,但我说“未解析的函数或方法”,如果您运行(从您的iframe)
    console.log(window.parent)
    ,控制台中会显示什么?您是否也尝试了
    postMessage