码迷,mamicode.com
首页 > 其他好文 > 详细

获取 iframe 里面的节点

时间:2021-05-24 03:18:17      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:mamicode   index   alt   console   elements   get   function   结果   方法   

获取 iframe 里面的节点

注意点:
1.一定要是同源
2.需要等iframe加载完了再去获取,否则就是空

//html
 <iframe
      src="http://localhost:3000/index2.html"
      name="iframe"
      id="iframe"
    ></iframe>

//script
  //window.frames[0] 另一种获取iframe的方法
      let iframe = document.getElementById("iframe");
      let innerDoc = iframe.contentDocument || iframe.contentWindow.document;
      console.log(innerDoc.getElementsByTagName("div"), "iframe未知");
      document.getElementById("iframe").onload = function () {
        let innerDoc = iframe.contentDocument || iframe.contentWindow.document;
        console.log(innerDoc.getElementsByTagName("div"), "iframe加载完了");
      };

结果如下
技术图片

获取 iframe 里面的节点

标签:mamicode   index   alt   console   elements   get   function   结果   方法   

原文地址:https://www.cnblogs.com/heihei-haha/p/14748074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!