|
復制代碼 代碼如下:
var temp_iframe
var content = document.getElementById('right'); //id為 right的DOM容器中,進行創(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,然后再前臺頁面中調(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,前臺:
------------------------------------------------------------------------
<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ù)來調(diào)用。
這個我沒去那么做,有看到文章的朋友,可自行封裝。
其中,還有一些不太方便的,比如我每次建立的iframe必須不同的名字和id,即時刪除原來已經(jīng)創(chuàng)建的也不行……
還是希望大家多多指教吧。
JavaScript技術(shù):iframe自適應(yīng)寬度、高度 ie6 7 8,firefox 3.86下測試通過,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。