最優(yōu)良人 » iframe http://www.dgkai.cn/blog 中山php|最優(yōu)網(wǎng)絡(luò) Mon, 13 May 2013 04:56:43 +0000 en hourly 1 http://wordpress.org/?v=3.1.4 讓框架iframe高度自適應(yīng),可自動(dòng)撐開高度 http://www.dgkai.cn/blog/view-236.html http://www.dgkai.cn/blog/view-236.html#comments Sun, 28 Aug 2011 07:46:30 +0000 lin http://www.dgkai.cn/blog/?p=236 默認(rèn)iframe框架是不能撐開高度的,如果需要讓框架高度自適應(yīng)需要用js實(shí)現(xiàn)
js代碼:
var adjustIframe = function (id) {
var iframe = document.getElementById(id)
var idoc = iframe.contentWindow && iframe.contentWindow.document || iframe.contentDocument;
var callback = function () {
var iheight = Math.max(idoc.body.scrollHeight, idoc.documentElement.scrollHeight); //取得其高
iframe.style.height = iheight + "px";
}
if (iframe.attachEvent) {
iframe.attachEvent("onload", callback);
} else {
iframe.onload = callback
}
}
HTML代碼如下所示:
使用代碼:

window.onload = function(){
adjustIframe("js_sub_web");
}

]]>
http://www.dgkai.cn/blog/view-236.html/feed 215