Resizing iframe to fit its content [duplicate]

2019-01-18 00:00发布

问题:

Possible Duplicate:
Resizing an iframe based on content

I have an iframe where the src is an HTML file and this iframe is put inside usercontrol:

<iframe frameborder="0" src="CName.htm" align="left" width="730" height="1100" ></iframe>

I need the iframe to resize according to the content so that it's height is set according to the hieght of the HTML file and I don't need to use scrolling attribute. Do you have any idea?

回答1:

I've used the following jQuery code to do this:

$('#iframe_id').load(function () {
    $(this).height($(this).contents().height());
    $(this).width($(this).contents().width());
});

I don't think this will work if the iframe contents are cross-domain, though.



回答2:

You can figure out the height and width of the content inside the frame, then call a function that's on the frame's owner to set the iframe element's height and width.



回答3:

There is a way! Its in jquery and its too simple. Kinda hacky..

$('iframe').contents().find('body').css({"min-height": "100", "overflow" : "hidden"});
setInterval( "$('iframe').height($('iframe').contents().find('body').height() + 20)", 1 );

There you go!

Cheers! :)



标签: iframe resize