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
