如何获取网页兄弟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();

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