转自:http://caiceclb.iteye.com/blog/281102

很高兴,终于使用jquery实现了点击外部链接,更改iframe内容时,iframe的高度自适应问题。

失败的测试就不说了,来直接的。

两个链接和iframe:

  1. <li><a href="selfinfo.jsp" target="c-c-iframe" title="个人信息" >个人信息</a></li>
  2. <li><a href="modifypass.jsp" target="c-c-iframe" title="修改密码" >修改密码</a></li>
  3. <iframe src="init.jsp" id="c-c-iframe" name="c-c-iframe" width="500px;"  frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>

js代码:

  1. <script type="text/javascript">
  2. <!--
  3. $(function(){
  4. $("#c-c-iframe").load(function(){
  5. $(this).height($(this).contents().find("#content").height() + 40);
  6. });
  7. });
  8. -->
  9. </script>

这里的find("#content")是找出iframe内容文档中的id为content的高度(另外比如find("body")),并设置给iframe, 
类似的还可以设置宽度,留给需要的朋友尝试吧。

这样就解决了iframe不会因为内容过大被挡住的问题(因为我设置了scrolling="no")。

PS:基本上我会优先考虑使用iframe来实现无刷新,兼容浏览器的后退按钮;而且使用iframe加载flash是很爽的,不用写什么js调用,object标签,还符合W3C标准。 
推荐下面这种
2008年11月28日17:13:31 ,今天使用过程中根据实际情况进行了一下改良,代码如下:

  1. <script type="text/javascript">
  2. <!--
  3. $(function(){
  4. $("#workArea").load(function(){
  5. var height = $(this).contents().find("#box").height() + 40;
  6. //这样给以一个最小高度
  7. $(this).height( height < 400 ? 400 : height );
  8. });
  9. });
  10. -->
  11. </script>

另发现使用find("body")不太好使,高度不准确。

最新文章

  1. Resharp注册码
  2. Win10 安装Vs2015 社区版和企业版各个问题汇总
  3. [Effective JavaScript 笔记]第27条:使用闭包而不是字符串来封装代码
  4. mysql 源码下载列表
  5. bzoj1391 最大权闭合子图(also最小割、网络流)
  6. fastscript调用delphi方法和DELPHI调用FASTSCRIPT方法
  7. Unity3D--学习太空射击游戏制作(三)
  8. 菜鸟的MySQL学习笔记(四)
  9. extjs两个tbar问题
  10. Gridlayout动态添加
  11. Hadoop基本命令详解
  12. 【译】The Accidental DBA:Troubleshooting
  13. avcodec_decode_video2()解码视频后丢帧的问题解决
  14. Eclipse使用JDBC小案例
  15. 事件冒泡(event bubbling)与事件捕捉(event capturing)
  16. update_engine-DownloadAction(一)
  17. 图解 Paxos 一致性协议
  18. Leetcode 5016. 删除最外层的括号
  19. CodeForces 2A Winner
  20. ABP 源码分析汇总之 IOC

热门文章

  1. node遍历给定目录下特定文件,内容合并到一个文件
  2. xmpp消息回执(6)
  3. Luogu P4549 裴蜀定理 / Min
  4. Idea+maven+testng+reportng生成测试报告
  5. Java第九周总结
  6. Batchelor Prize
  7. naca0012
  8. [luoguP1433] 吃奶酪(DP || Dfs)
  9. [bzoj2097][Usaco2010 Dec]Exercise 奶牛健美操_贪心_树形dp_二分
  10. SAP Portal 上传资源到WRR