使用Js方法,如何获取网页兄弟iframe里的元素,如何调用网页兄弟iframe里的事件?
一、案例:
....
<body>
<iframe id="a1"><input type="button" id="btn_Search" onclick=""/></iframe>
<iframe id="a2"></iframe>
</body>
....
二、使用Js方法,在a2页面,获取a1页面的元素
//找兄弟a1的iframe
let iframe = parent.document.getElementById("a1");
//找iframe里的document(某些浏览器不支持contentDocument,需结合contentWindow.document使用)
let iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
//对iframe里的元素操作(有时候能找到,但不触发,需要重新打开网站)
iframeDocument.getElementById("btn_Search").click();
本文介绍了一种使用JavaScript从一个iframe访问另一个同级iframe中的元素的方法。通过获取父文档中的iframe元素,进而操作其内部的内容文档,实现对目标iframe内元素的操作,如点击按钮等。
1599

被折叠的 条评论
为什么被折叠?



