使用Js方法,如何获取网页兄弟iframe里的元素,如何调用网页兄弟iframe里的事件?
一、案例:
....
<body>
<iframe id="a1"><input type="button" id="btn_Search" οnclick=""/></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();