document獲取iframe


1.獲取頁面上的所有iframe標簽遍歷獲取每一個iframe

獲取所有的iframe

let iframes = document.getElementsByTagName("iframe");

//遍歷iframes

for (let i = 0; i < iframes.length; i++) {
let iframeId = iframes[i].id;
let currentIframe = document.getElementById(iframeId);
}
//取完整iframe元素 再用 getElementById的方法獲取。

獲取iframe下document元素

//這里主要拿到iframe的document操作元素,有些瀏覽器可以直接contentDocument獲取document操作元素,有些需要通過contentWindow.document獲取
let currentDoc = currentIframe.contentDocument || currentIframe.contentWindow.document  

獲取iframe中輸入框

//這樣就能獲取iframe所有的輸入框標簽。
let inputs = currentDoc.getElementsByTagName("input");

*注意:當iframe跨域的時候,就無法獲取iframe的document操作。  

參考鏈接:https://www.cnblogs.com/lcspring/p/10850365.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM