假设我们在当前页面要嵌套一个iframe
1
|
< iframe id = "myframe" src = "test.html" height = "240" width = "100%" frameborder = "0" scrolling = "auto" ></ iframe >
|
那么我们在test.html中的代码如下
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<! DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< title >jQuery实现iframe的自适应高度</ title >
< meta name = "keywords" content = "" />
< meta name = "description" content = "" />
< meta name = "viewport" content = "width=device-width" />
< script type = "text/javascript" src = "http://code.jquery.com/jquery-latest.js" ></ script >
< script >
//注意:下面的代码是放在test.html调用,并且id跟test.html中的iframe的id要保持一致 $(window.parent.document).find("#myframe").load(function(){ var main = $(window.parent.document).find("#myframe");
var thisheight = $(document).height()+30;
main.height(thisheight);
}); </ script >
</ head >
< body >
< div style = "height:300px;background:#ff0000" >我是来测试高度的</ div >
</ body >
</ html >
|
演示效果
转载请注明来源:Web前端(W3Cways.com) - Web前端学习之路 » jQuery实现iframe的自适应高度