使iframe随内容(target到iframe的内容)改变而自适应高度,完美解决各种获取第一个demo高度后第二个高度不变情况
2024-08-28 18:13:40
转自:http://caiceclb.iteye.com/blog/281102
很高兴,终于使用jquery实现了点击外部链接,更改iframe内容时,iframe的高度自适应问题。
失败的测试就不说了,来直接的。
两个链接和iframe:
- <li><a href="selfinfo.jsp" target="c-c-iframe" title="个人信息" >个人信息</a></li>
- <li><a href="modifypass.jsp" target="c-c-iframe" title="修改密码" >修改密码</a></li>
- <iframe src="init.jsp" id="c-c-iframe" name="c-c-iframe" width="500px;" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>
js代码:
- <script type="text/javascript">
- <!--
- $(function(){
- $("#c-c-iframe").load(function(){
- $(this).height($(this).contents().find("#content").height() + 40);
- });
- });
- -->
- </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 ,今天使用过程中根据实际情况进行了一下改良,代码如下:
- <script type="text/javascript">
- <!--
- $(function(){
- $("#workArea").load(function(){
- var height = $(this).contents().find("#box").height() + 40;
- //这样给以一个最小高度
- $(this).height( height < 400 ? 400 : height );
- });
- });
- -->
- </script>
另发现使用find("body")不太好使,高度不准确。
最新文章
- Resharp注册码
- Win10 安装Vs2015 社区版和企业版各个问题汇总
- [Effective JavaScript 笔记]第27条:使用闭包而不是字符串来封装代码
- mysql 源码下载列表
- bzoj1391 最大权闭合子图(also最小割、网络流)
- fastscript调用delphi方法和DELPHI调用FASTSCRIPT方法
- Unity3D--学习太空射击游戏制作(三)
- 菜鸟的MySQL学习笔记(四)
- extjs两个tbar问题
- Gridlayout动态添加
- Hadoop基本命令详解
- 【译】The Accidental DBA:Troubleshooting
- avcodec_decode_video2()解码视频后丢帧的问题解决
- Eclipse使用JDBC小案例
- 事件冒泡(event bubbling)与事件捕捉(event capturing)
- update_engine-DownloadAction(一)
- 图解 Paxos 一致性协议
- Leetcode 5016. 删除最外层的括号
- CodeForces 2A Winner
- ABP 源码分析汇总之 IOC
热门文章
- node遍历给定目录下特定文件,内容合并到一个文件
- xmpp消息回执(6)
- Luogu P4549 裴蜀定理 / Min
- Idea+maven+testng+reportng生成测试报告
- Java第九周总结
- Batchelor Prize
- naca0012
- [luoguP1433] 吃奶酪(DP || Dfs)
- [bzoj2097][Usaco2010 Dec]Exercise 奶牛健美操_贪心_树形dp_二分
- SAP Portal 上传资源到WRR