欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS操作iframe里的dom(實(shí)例講解)

 更新時(shí)間:2014年01月29日 10:00:19   作者:  
本篇主要是對(duì)JS操作iframe里的dom進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助

直接賦值如下代碼測(cè)試即可明白:

1.html:

復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無(wú)標(biāo)題文檔</title>
</head>

<body>
<div class="line">====================注意:測(cè)試從這里開(kāi)始=========================</div>
<p id="pox">用來(lái)測(cè)試子窗體iframeA訪問(wèn)父窗體的某元素</p>
<div class="line">====================iframe分割線=========================</div>
<iframe src="a.html" width="100%" frameborder="0" id="frameA" name="frameA"></iframe>
<iframe src="b.html" name="iframeB" width="100%" frameborder="0" id="frameB"></iframe>
<div class="line">====================iframe分割線=========================</div>
<p>先來(lái)演示:父窗體訪問(wèn)子窗體中的某方法或元素</p>
<p>總結(jié):父窗體訪問(wèn)子窗體的方法跟元素采用不同的方式</p>
<input type="button" onclick="frameDiv()" value="父窗體訪問(wèn)子窗體中的某元素" />
<input type="button" onclick="frameFun()" value="父窗體訪問(wèn)子窗體中的某方法" />
<script type="text/javascript">
    //子窗口訪問(wèn)父窗口方法
    function testP(ss){
        alert(ss)
    }
    //取得iframe的元素
    function getIframe(id){
        return document.getElementById(id).contentWindow.document;
    }
    //父窗口訪問(wèn)子窗口元素
    function frameDiv(){
        getIframe("frameA").getElementById("ooxx").style.backgroundColor="#f00"
        //window.frames["iframeA"].getElementById("ooxx").style.backgroundColor="#f00"  //不能通過(guò)這種形式訪問(wèn)某元素
    }
    //父窗口訪問(wèn)子窗口方法
    function frameFun(){
        //getIframe("frameB").getsFun();//不能通過(guò)這種形式訪問(wèn)子窗體某方法
       // window.frames["iframeB"].getsFun();
  alert(window.frames["iframeB"].getsFun());
    }
</script>
</body>
</html>


a.html
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無(wú)標(biāo)題文檔</title>
</head>

<body>
<div id="ooxx">用來(lái)測(cè)試父窗體訪問(wèn)子窗體中的某元素</div>
<p id="divooxx">用來(lái)測(cè)試子窗口B訪問(wèn)窗體A的某元素</p>
<p>1.子窗口iframeA訪問(wèn)父窗口的某元素</p>
<input type="button" onclick="frameToPdiv()" value="子窗口訪問(wèn)父窗口的某元素" />
<input type="button" onclick="frameToPfun()" value="子窗口訪問(wèn)父窗口的某方法" />
<script type="text/javascript">
    //子窗口訪問(wèn)父窗口的某元素
    function frameToPdiv(){
        parent.document.getElementById("pox").style.color="#fff";
        parent.document.getElementById("pox").style.backgroundColor="#f0a0f0"
    }
    //子窗口訪問(wèn)父窗口方法
    function frameToPfun(ss){
        parent.testP("ssss");
    }
    //用于測(cè)試iframeB訪問(wèn)的方法
    function testBA(){
        alert("用于測(cè)試iframeB訪問(wèn)的方法")
    }
</script>
</body>
</html>


b.html
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無(wú)標(biāo)題文檔</title>
</head>

<body>
<p>二:測(cè)試子窗體間相互訪問(wèn)某方法或元素</p>
<input type="button" value="子窗體B訪問(wèn)子窗體A的某元素" onclick="frameTframeDiv()" />
<input type="button" value="子窗體B訪問(wèn)子窗體A的某方法" onclick="frameTframeFun()" />
<script type="text/javascript">
    //子窗體B訪問(wèn)子窗體A的某元素
    function frameTframeDiv(){
        //parent.document.getElementById("frameA").contentWindow.document.getElementById("divooxx").style.color="#a0c0f0";
        //parent.document.getElementById("frameA").contentWindow.document.getElementById("divooxx").style.backgroundColor="#000"
        var _bframe=parent.getIframe("frameA");//子窗體訪問(wèn)父窗體方法
        _bframe.getElementById("divooxx").style.color="#a0c0f0";
        _bframe.getElementById("divooxx").style.backgroundColor="#000";
    }
    //子窗體B訪問(wèn)子窗體A的某方法
    function frameTframeFun(){
            window.parent.frames["frameA"].testBA();
    }
</script>
<script type="text/javascript">
    function getsFun(){
        return "sssssss";
    }
    //getFun()
</script>
</body>
</html>

相關(guān)文章

最新評(píng)論