如何獲取網頁兄弟iframe裏的元素,如何調用網頁兄弟iframe裏的事件

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章