iframe框架用JavaScript子頁(yè)面控制父頁(yè)面parent.html 中的代碼為: <iframe marginwidth="0" framespacing="0" marginheight="0" frameborder="0"
name="uploadframe" id="uploadframe" src="c.html" scrolling="no" width="100" height="100" ></iframe> 如想在c.html 中寫一些代碼去改變parent.html 中的一些內(nèi)容,以下代碼可作為參考: 1、parent.window.frames 可返回parent.html 中所有的iframe;返回結(jié)果應(yīng)該是一個(gè)數(shù)組,用parent.window.frames[iframeId]可得到iframeId; 2、用parent.document.getElementById('xxxx')可得到父里的xxxx,并改變相應(yīng)的值,例如:parent.document.getElementById('xxxx').className = 'test'; 3、如果我想在父中再創(chuàng)建一個(gè)元素,直接用parent.appendChild(yyyy)在firefox中是可以的,但在IE(最起碼IE6)是不行的; 所以,要把創(chuàng)建這個(gè)動(dòng)作放在父中來(lái)完成,在子中調(diào)用; 例如:父中的代碼為: function addIframe(vNum){
var iframe = document.createElement("iframe"); iframe.setAttribute("marginwidth", "0"); iframe.setAttribute("framespacing", "0"); iframe.setAttribute("marginheight", "0"); iframe.setAttribute("frameborder", "0"); iframe.setAttribute("name", vNum); iframe.setAttribute("id", vNum); iframe.setAttribute("src", "http://www."); iframe.setAttribute("scrolling", "no"); iframe.setAttribute("width", "100"); iframe.setAttribute("height", "100"); document.getElementsByTagName('body')[0].appendChild(iframe); return; } 子中可以這樣調(diào)用: <script>
parent.addIframe('xxxx'); </script> 這樣就能完整創(chuàng)建一個(gè)元素了; 4、同理,如果我想在子中把iframe給隱藏,本來(lái)想直接用parent.document.getElementById('uploadframe').style.display = 'none';來(lái)弄,但無(wú)論是在IE還是FIREFOX中,都是行不通的;只能用上邊的方法把動(dòng)作放在父中。 |
|
來(lái)自: 趨明 > 《編程開發(fā)》