vue嵌套iframe问题

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

router.go()的失效问题

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

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

  解决办法:

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

<!-- A.html -->
 
<template>
 
<iframe ref="iframe" scrolling="auto" width="100%" height="100%" frameborder="0" ></iframe>
 
</template>
 
<script>
 
 
this.$refs.iframe.contentWindow.location.replace(val)


</script>

这有一个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的宽

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值