Javascript 显示和隐藏根据内容调整大小的iframe

Javascript 显示和隐藏根据内容调整大小的iframe,javascript,jquery,Javascript,Jquery,嘿,我有一个iframe,我正在使用下面的脚本调整它的大小。我想用两个按钮显示和隐藏此iframe,但如果它被隐藏,则不会计算iframe的宽度和高度,因此当我单击“显示”按钮时,它不会显示,因为宽度和高度仍设置为0 我对jquery和javascript非常陌生,因此非常感谢您的帮助 <script language="JavaScript"> $(document).ready(function(){ $(".ShowFrame").click(function(){

嘿,我有一个iframe,我正在使用下面的脚本调整它的大小。我想用两个按钮显示和隐藏此iframe,但如果它被隐藏,则不会计算iframe的宽度和高度,因此当我单击“显示”按钮时,它不会显示,因为宽度和高度仍设置为0

我对jquery和javascript非常陌生,因此非常感谢您的帮助

<script language="JavaScript">
$(document).ready(function(){
    $(".ShowFrame").click(function(){
        $("#iframe1").show();
    });
    $("#iframe1").hide();
});

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";
}
</script>

$(文档).ready(函数(){
$(“.ShowFrame”)。单击(函数(){
$(“#iframe1”).show();
});
$(“#iframe1”).hide();
});
函数自动调整大小(id){
var newheight;
新宽度;
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在同一线程上运行,而JavaScript在执行web页面时无法重新绘制

$("#iframe1").show();
autoResize();
$("#iframe1").hide();

可以在重新绘制网页之前显示、计算尺寸,然后再次隐藏。这是因为呈现引擎和JavaScript在同一线程上运行,而JavaScript在执行web页面时无法重新绘制

$("#iframe1").show();
autoResize();
$("#iframe1").hide();

您可以在ShowFrame单击上调用resize函数

$(document).ready(function(){ 
    $(".ShowFrame").click(function(){ 
        $("#iframe1").show(); 
        autoResize("iframe1"); 
    }); 
    $("#iframe1").hide(); 
});

您可以在ShowFrame单击上调用resize函数

$(document).ready(function(){ 
    $(".ShowFrame").click(function(){ 
        $("#iframe1").show(); 
        autoResize("iframe1"); 
    }); 
    $("#iframe1").hide(); 
});

非常感谢!高德·斯塔克沃德!非常感谢!高德·斯塔克沃德!