Javascript/Jquery:自动调整iframe的高度(如何在需要时缩小iframe) [英] Javascript/Jquery: Auto adjust height of iframe (how to make iframe smaller when needed)

查看:75
本文介绍了Javascript/Jquery:自动调整iframe的高度(如何在需要时缩小iframe)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,一旦文本超过iframe可见高度,iframe就会变高;但是,删除文字后,iframe会保持相同的高度.如何修改此代码(或是否有其他代码),以将iframe调整为恰好合适的高度.我需要将此功能放入函数中,以便在键入命令时可以运行该函数.

I have the following code that makes the iframe taller once text exceeds iframe visible height; however, when text is removed, the iframe remains just as tall. How do I modify this code (or if there is a different code) that will adjust the iframe to be JUST the right height. I need this to be in a function so that on keyup I can run the function.

function autoResize(id){
    var newheight;
    var newwidth;

    if(document.getElementById){
        newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
        newwidth=document.getElementById(id).contentWindow.document .body.scrollWidth;
    }

    document.getElementById(id).height= (newheight) + "px";
    document.getElementById(id).width= (newwidth) + "px";
}

推荐答案

免责声明:不确定以下解决方案是否是最佳解决方案.

Disclaimer: Not sure if below solution is the best one.

您可以将iframe重置为标准尺寸,然后使用您的方法调整其大小.

You can reset iframe to your standard size and then resize it using your method.

示例:

如果iframe的标准尺寸为300px x 300px

If the standard size of your iframe is 300px by 300px

function autoResize(id){
   //Reset iframe size
   document.getElementById(id).height= "300px";
   document.getElementById(id).width= "300px";

   //Your method continues
   var newheight;
   var newwidth;

   if(document.getElementById){
       newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
       newwidth=document.getElementById(id).contentWindow.document .body.scrollWidth;
   }

   document.getElementById(id).height= (newheight) + "px";
   document.getElementById(id).width= (newwidth) + "px";
}

希望这会有所帮助

这篇关于Javascript/Jquery:自动调整iframe的高度(如何在需要时缩小iframe)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆