|
復(fù)制代碼 代碼如下:
var temp_iframe
var content = document.getElementById('right'); //id為 right的DOM容器中,進(jìn)行創(chuàng)建iframe和寬高自適應(yīng)
var c = 0;
function append(filename)
{
var the_iframe = "helpfile" + c;
temp_iframe = document.createElement("iframe");
temp_iframe.src = filename;
temp_iframe.scrolling = "no";
temp_iframe.setAttribute("frameborder", "0");
temp_iframe.id = the_iframe;
temp_iframe.name = the_iframe;
scroll(0, 0);
content.innerHTML = "";
content.appendChild(temp_iframe);
if (document.all)
{
temp_iframe.attachEvent('onload', function()
{
temp_iframe.setAttribute("width", window.frames[the_iframe].document.body.scrollWidth); //自適應(yīng)寬
temp_iframe.setAttribute("height", window.frames[the_iframe].document.body.scrollHeight); //自適應(yīng)高
});
}
else
{
temp_iframe.addEventListener('load', function()
{
temp_iframe.setAttribute("width", window.frames[the_iframe].document.body.scrollWidth);
temp_iframe.setAttribute("height", window.frames[the_iframe].document.body.scrollHeight);
}, false);
}
c++;
return false;
}
/*調(diào)用方法
把此腳本另存為腳本文件:iframe.js,然后再前臺(tái)頁(yè)面中調(diào)用:
------------------------------------------------------------------------
<a href="index.html" onclick="return append('xxxx.html')" class="li1">
<div id="right"></div>
<script type="text/Javascript" src="iframe.js"></script>
------------------------------------------------------------------------
或者 去掉腳本中最后的 return false,前臺(tái):
------------------------------------------------------------------------
<a href="Javascript:append('favorite/Favorites.html')" class="li1">
<div id="right"></div>
<script type="text/Javascript" src="iframe.js"></script>
------------------------------------------------------------------------
*/
嘿嘿,其中,最后設(shè)置自適應(yīng)寬高的,其實(shí)可以提出來(lái)成為一個(gè)函數(shù)來(lái)調(diào)用。
這個(gè)我沒(méi)去那么做,有看到文章的朋友,可自行封裝。
其中,還有一些不太方便的,比如我每次建立的iframe必須不同的名字和id,即時(shí)刪除原來(lái)已經(jīng)創(chuàng)建的也不行……
還是希望大家多多指教吧。
JavaScript技術(shù):iframe自適應(yīng)寬度、高度 ie6 7 8,firefox 3.86下測(cè)試通過(guò),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。