AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > JavaScript

JS未跨域操作iframe里的DOM

51自学网 http://www.wanshiok.com
js,iframe,js未跨域操作ifram,dom

这里简单说明两个方法,都是未跨域情况下在index.html内操作b.html内的 DOM。

如:index.html内引入iframe,在index内如何用JS操作iframe内的DOM元素?

先贴下index.html和iframe引入的a.html内容。

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动态创建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"));}; 

两种的输出结果都是

以上所述是小编给大家介绍的JS未跨域操作iframe里的DOM 的相关知识,希望对大家有所帮助!


js,iframe,js未跨域操作ifram,dom  
上一篇:Bootstrap 附加导航(Affix)插件实例详解  下一篇:jQuery实现的简单分页示例