src可以是相對URL,如src="/bgm/bgm.html"
....................................................................................
2.如何使iframe背景透明
在transparentBody.htm文件的<body>標簽中,我已經加入了style="background-color=transparent" 屬性,通過以下四種iframe的寫法我想大概你對iframe背景透明效果的實現方法應該會有個清晰的了解:
<iframe id="Frame1" src="http://www.webjx.com/htmldata/2006-01-03/transparentBody.htm" allowTransparency="true"></iframe>
<iframe id="Frame2" src="http://www.webjx.com/htmldata/2006-01-03/transparentBody.htm" allowTransparency="true" style="background-color: green"> </iframe>
<iframe id="Frame4" src="http://www.webjx.com/htmldata/2006-01-03/transparentBody.htm" style="background-color: green"> </iframe>
.....................................................................................
3.如何實現iframe自適應窗體大小
<script>
function autoResize()
{
try
{
document.all["content"].style.height=content.document.body.scrollHeight
}
catch(e){}
}
</script>
<iframe src="main.htm" name="content" id="content" scrolling="no" frameborder="0" width="100%" onload="autoResiz();"> </iframe>
.........................
4.iframe 父窗口和子窗口的調用方法
在腳本語言與對象層次中,包含Iframe的窗口我們稱之為父窗體,而浮動幀則稱為子窗體,弄清這兩者的關系很重要,因為要在父窗體中訪問子窗體或相反都必須清楚對象層次,才能通過程序來訪問并控制窗體。
父窗口調用子窗口的例子:
exmaple.htm
<html>
<body onload="test.myH1.innerText='hello,my dear';">
<Iframe src="test.htm" id="test" width="250" height="200" scrolling="no" frameborder="0"></iframe>
</body>
</html>
test.htm
<html>
<body>
<h1 id="myH1">hello,my boy</h1>
</body>
</html>
子窗口調用父窗口的例子:
example.htm:
<html>
<body>
<Iframe name="tt" src="frame1.htm" width="250" height="200" scrolling="no" frameborder="0"></iframe>
<h1 id="myH2">hello,my wife</h1>
</body>
</html>
frame1.htm
<body onload="parent.myH2.innerText='hello,my new wife';"></body>
要注意的是,Nestscape6.0之前版本不支持Iframe標記。
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
5.用圖片代替Iframe的滾動條
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>new document</title>
<script language="javascript">
function scroll(n)
{temp=n;
Out1.scrollTop=Out1.scrollTop+temp;
if (temp==0) return;
setTimeout("scroll(temp)",80);
}
</script>
</head>
<body>
<table width="330">
<tr>
<td width="304" valign="top" rowspan="2">
<div id=Out1 style="width:100%; height:100;overflow: hidden ;border-style:dashed;border-width: 1px,1px,1px,1px;">
1<br>
2<br>
3<br>
4<br>
5<br>
6<br>
7<br>
8<br>
9<br>
10<br>
11<br>
12<br>
13<br>
14<br>
15<br>
16<br>
17<br>
18<br>
19<br>
20<br>
</div>
</td>
<td width="14" valign="top"><img src="/blog/scrollbar.gif" width="14" height="20" onmouseover="scroll(-1)" onmouseout="scroll(0)" onmousedown="scroll(-2)" border="0" alt="按下鼠標速度會更快!"></td>
</tr>
<tr>
<td width="14" valign="bottom"><img src="/blog/scrollbar.gif" onmouseover="scroll(1)" onmouseout="scroll(0)"
onmousedown="scroll(2)" border="0" width="15" height="21" alt="按下鼠標速度會更快!"></td>
</tr>
</table>
</body>
</html>
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
6.下面這段代碼可以實現IFrame自適應高度,即隨著頁面的長度,自動適應以免除頁面和IFrame同時出現滾動條(比上面的一個復雜)。
源代碼如下:
<script type="text/javascript">
//** iframe自動適應頁面 **//
//輸入你希望根據頁面高度自動調整高度的iframe的名稱的列表
//用逗號把每個iframe的ID分隔. 例如: ["myframe1", "myframe2"],可以只有一個窗體,則不用逗號。
//定義iframe的ID
var iframeids=["test"]
//如果用戶的瀏覽器不支持iframe是否將iframe隱藏 yes 表示隱藏,no表示不隱藏
var iframehide="yes"
function dyniframesize()
{
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++)
{
if (document.getElementById)
{
//自動調整iframe高度
dyniframe[dyniframe.length] = document.getElementById(iframeids);
if (dyniframe && !window.opera)
{
dyniframe.style.display="block"
if (dyniframe.contentDocument && dyniframe.contentDocument.body.offsetHeight) //如果用戶的瀏覽器是NetScape
dyniframe.height = dyniframe.contentDocument.body.offsetHeight;
else if (dyniframe.Document && dyniframe.Document.body.scrollHeight) //如果用戶的瀏覽器是IE
dyniframe.height = dyniframe.Document.body.scrollHeight;
}
}
//根據設定的參數來處理不支持iframe的瀏覽器的顯示問題
if ((document.all || document.getElementById) && iframehide=="no")
{
var tempobj=document.all? document.all[iframeids] : document.getElementById(iframeids)
tempobj.style.display="block"
}
}
}
if (window.addEventListener)
window.addEventListener("load", dyniframesize, false)
else if (window.attachEvent)
window.attachEvent("onload", dyniframesize)
else
window.onload=dyniframesize
</script>
::::::::::::::::::::::::::::::::::::::::::::::
7.iframe應用之動態獲取并替換網頁內容
經常上論壇都能看的到,如果點擊某個主題會出現“正在讀取改貼的跟貼,請稍后.....”等的字眼。之后就顯示了該主題的跟貼的一些相關信息。由于這幾天要做類似于這種方式的BBS,所以就拿了一個BBS網站來研究一把,經過半天的查看源碼和實踐終于弄懂了。
它主要的是Iframe和innerHTML結合的結晶。下面是一個簡單的示范:
page1.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>page1</title>
</head>
<body>
<script language="javascript">
function loadContent(){
//顯示隱藏的提示內容
document.all.tr2.style.display="block";
//重新刷新iframe1的內容
document.frames["iframe1"].location.replace("page2.htm");
}
</script>
<!--精華所在,用隱藏的iframe來作為外部數據的來源-->
<iframe id="iframe1" width="0" height="0" name="I1"></iframe>
<table>
<tr id="tr1" ><!--DISPLAY: none是把這行隱藏-->
<td style="cursor:hand" onclick="loadContent()">
點擊這里你會有新發現。。。
</td>
</tr>
<tr id="tr2" style="DISPLAY: none" ><!--DISPLAY: none是把這行隱藏-->
<td>
<div id="div_hidden">正在讀取關于本主題的跟貼,請稍侯……</div>
</td>
</tr>
</table>
</body>
</html>
page2.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>page2</title>
</head>
<body>
<script language="javascript">
alert("準備替換提示內容!");
parent.div_hidden.innerHTML = "你已經看到從另外一個頁面來的替換信息";
</script>
</body>
</html>