這里簡(jiǎn)單說(shuō)明兩個(gè)方法,都是未跨域情況下在index.html內(nèi)操作b.html內(nèi)的 DOM。
如:index.html內(nèi)引入iframe,在index內(nèi)如何用JS操作iframe內(nèi)的DOM元素?
先貼下index.html和iframe引入的a.html內(nèi)容。
index-><div class="d1"><iframe src="a.html" frameborder="0" name="one" id="iframeId"></iframe></div>
a.html
<div id="dd"><h1>iframe里的元素!</h1></div>
法一:
var d=window.frames["one"].window;d.onload=function(){console.log(d.document.getElementById("dd"));}; 法二:
JS動(dòng)態(tài)創(chuàng)建iframe并插入
var ifr = document.createElement('iframe');ifr.src = 'a.html';document.body.appendChild(ifr);ifr.onload = function(){var doc = ifr.contentDocument || ifr.contentWindow.document;// 在這里操縱b.htmlconsole.log(doc.getElementById("dd"));}; 兩種的輸出結(jié)果都是

以上所述是小編給大家介紹的JS未跨域操作iframe里的DOM 的相關(guān)知識(shí),希望對(duì)大家有所幫助!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注