国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 16:00:15
字體:
來源:轉載
供稿:網友
本篇主要是對JS操作iframe里的dom進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助

直接賦值如下代碼測試即可明白:

1.html:

復制代碼 代碼如下:


<!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>無標題文檔</title>
</head>

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


a.html

復制代碼 代碼如下:


<!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>無標題文檔</title>
</head>

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


b.html

復制代碼 代碼如下:


<!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>無標題文檔</title>
</head>

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 清水县| 扎兰屯市| 朝阳县| 都兰县| 余姚市| 牟定县| 瑞昌市| 枝江市| 宁德市| 安徽省| 勃利县| 嘉义县| 仁寿县| 山阴县| 始兴县| 伊宁市| 津市市| 和硕县| 天水市| 宁国市| 会昌县| 济南市| 德安县| 安新县| 霍城县| 上蔡县| 铜梁县| 乐业县| 大安市| 大港区| 西充县| 喀喇沁旗| 民乐县| 汾西县| 孝感市| 台中县| 渭南市| 吴旗县| 汉源县| 日照市| 依兰县|