博客
关于我
vue嵌套iframe问题
阅读量:620 次
发布时间:2019-03-12

本文共 2721 字,大约阅读时间需要 9 分钟。

最近在项目中遇到一个需求需要在一个项目中直接引用另一个项目,尝试各种情况无果后选择了iframe。现将调用过程中遇到的问题做一个分享。

router.go()的失效问题

  此情况主要适用于更改iframe中src值以后导致的路由跳转混乱。

  详细描述:当多次更改iframe->src属性后,调用router.go(-1),不能实现路由后退上一级,而是将iframe当作一个窗口文档,调用了该窗口文档的window.history.go(-1),并未更改父级项目的路由后退功能。

  解决办法:

  不通过改变iframe->src属性值去访问具体内容,采用window.location.replace(url)更改iframe将访问的内容,具体代码如下:

这有一个iframe标签,src指向的是一个路径(图片路径)

在这里插入图片描述

在浏览器上用审查元素看一下:1.iframe 2.#document 3.html 4.head 5.body 6.body.img
在这里插入图片描述
那么问题来了,怎么给iframe里边的img设置宽和高呢?

  1. let iframe =.document.getElementById(“preview”);//通过id找到想要元素
  2. let iwindow = iframe.contentWindow;//获取iframe的window对象
  3. 以下很是致命,切记:在iframe加载完毕才能拿到里面的所有document树(代码第五行)
  4. let idoc = iwindow.document; //获取iframe的document
  5. let img = idoc.getElementsByTagName(‘img’)[0];//搜索指定元素的后代,不包括自身,【0】第一个’img’
  6. img.width = iframe.clientWidth;将iframe的宽赋值给图片,高也是一样。
let iframe = document.getElementById("preview");//通过id找到想要元素                console.log("iframe",iframe);                let iwindow = iframe.contentWindow;                                iframe.addEventListener('load',function () {                // 在iframe加载完毕才能拿到里面的所有document树                //很重要,必须放置在load函数里                //切记,坑深勿踩                    let idoc = iwindow.document;                    console.log("iwindow", iwindow);                    console.log("idoc", idoc);                    let img = idoc.getElementsByTagName('img')[0];                    if (img) {                        if(img.width >= img.height){                            img.width = iframe.clientWidth;                            let top = (iframe.clientHeight - img.height) / 2;                            img.style.marginTop = top + "px";                        }else {                            img.height = iframe.clientHeight;                            let left = (iframe.clientWidth - img.width) / 2;                            img.style.marginLeft = left + "px";                        }                    }   })

 clientWidth = width+左右padding;

clientHeigh = height + 上下padding ;
clientTop = boder.top(上边框的宽度);
clientLeft = boder.left(左边框的宽度);
marginTop:设置一个p元素的上部边距
marginLeft:设置一个p元素的左部边距

以下内容可加在合适的位置控制台输出查看

// console.log("window",iwindow);//获取iframe的window对象                    // console.log("document",idoc);  //获取iframe的document                    // console.log("html",idoc.documentElement);//获取iframe的html                    // console.log("head",idoc.head);  //获取head                    // console.log("body",idoc.body);  //获取body                    // console.log("img",idoc.getElementsByTagName('img'[0]));//获取img                    // console.log("iframeWidth",iframe.clientWidth);//获取iframe的宽                    // console.log("imgWidth",idoc.body.getElementsByTagName('img'[0]));//获取img的宽

 

转载地址:http://flrxz.baihongyu.com/

你可能感兴趣的文章
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>