html是这样的
<iframe id="MainFrame">
<div id="Maintest">This is main text
<div id ="SecondTest">This is second layer
<div id="ThirdTest">This is third layer</div>
</div>
</div>
</iframe>
访问框架中元素的javascript是这样的
var iframe = document.getElementById('MainFrame');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
console.log(iframe);
var mydiv = innerDoc.getElementById("Maintest");
console.log(mydiv);
var seconddiv = innerDoc.getElementById("SecondTest");
var thirddiv = innerDoc.getElementById("ThirdTest");
console.log(seconddiv+thirddiv);
mydiv、seconddiv 和thirddiv 给null。我在很多帖子上看到这是访问iframe 元素的方式。我不知道为什么它给null。感谢帮助。
谢谢斯瓦拉吉