使用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();