js获取iframe的window对象(包括iframe中不存在name和id的情况)

js获取iframe中的元素以及在iframe中获取父级的元素(包括iframe中不存在name和id的情况)

第一种情况:iframe中不存在name和id的方法:(通过contentWindow获取)

<iframe src="about:_blank" id="iframeId"></iframe>
<script>
document.getElementById('iframeId').contentWindow
</script>

第二种iframe里面有name属性

<iframe src="about:_blank" name="iframeId"></iframe>
<script>
// 得到的直接就是window对象
window.frames["iframeId"]
</script>

如果这篇文章对您有帮助,您可以打赏我

技术交流QQ群:15129679

原文地址:https://www.cnblogs.com/yeminglong/p/15001732.html