iframe高度自适应
生活随笔
收集整理的這篇文章主要介紹了
iframe高度自适应
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
iframe現在用的不多了,不過很多項目中還是需要用的到的,看到就轉載過來了,再學習下。
高度自適應代碼代碼如下:
<iframe src="test 2.html" width=“100%” height="600" id="iframe1" οnlοad="iFrameHeight('iframe1')" name="iframe1"></iframe> <iframe src="test 2.html" id="iframe2" οnlοad="iFrameHeight('iframe2')" name="iframe2"></iframe> <iframe src="test 2.html" id="iframe3" οnlοad="iFrameHeight('iframe3')" name="iframe3"></iframe> <iframe src="test 2.html" id="iframe4" οnlοad="iFrameHeight('iframe4')" name="iframe4"></iframe> </body> <script> console.log(document.getElementById("iframe3").contentWindow); function iFrameHeight(id) {var ifm= document.getElementById(id);var subWeb = document.frames ? document.frames[id].document :ifm.contentDocument;if(ifm != null && subWeb != null) {ifm.height = subWeb.body.scrollHeight;} } </script>?
關于test 2.html就是你要預覽的pdf頁面,可以很長!
注意事項:
1、上述代碼必須在localhost等服務器環境下面預覽
2、test 2.html也就是iframe的src必須是同域下面
代碼解釋
document.getElementById("iframe3").contentWindow?
可以獲取iframe的window對象。谷歌瀏覽器可以通過
document.frames[id].document 獲取document對象Firefox 支持, ie8 以上的ie可以通過
document.getElementById(id).contentDocument 來獲取document對象。其他解釋
其實操作iframe的方法還有很多!例如jquery有個contents()方法,可以查找iframe中的某個id或者某段內容。
例如:
$("#iframe2").contents().find("someID").html() 或者 $("#mainiframe").contains().find("someID").text()?
iframe2中包含某個ID。
也可以js和jquery混著用!例如:
$(window.frames["iframe1"].document).find("input[@type='radio']").attr("checked","true");?
?
原文:http://www.haorooms.com/post/ifame_height_zishiying
轉載于:https://www.cnblogs.com/koma2014/p/5651339.html
總結
以上是生活随笔為你收集整理的iframe高度自适应的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java调用python代码
- 下一篇: Djaongo 中间件