vue组件中如何给iframe标签中的img设置宽和高

这有一个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元素的左部边距

 <div class="down">
                    <iframe :src="src" id="preview" width="740px" height="480px"></iframe>
                </div>

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

                    // 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的宽
要修改Vueiframe的背景颜色,你可以采取以下步骤: 1. 首先,在Vue组件找到包含iframe标签或元素,并为其添加一个唯一的id,方便后续操作。例如,你可以给iframe所在的div元素添加一个id="my-iframe"。 2. 接下来,在Vue组件的methods选项编写一个方法,用于修改iframe的背景颜色。你可以称此方法为changeIframeBgColor()。 3. 在这个方法,你可以通过获取到iframe的DOM元素,使用Vue的$refs属性来实现。在changeIframeBgColor()方法添加以下代码: ```javascript changeIframeBgColor() { const iframeElement = this.$refs.myIframe.contentDocument.body; iframeElement.style.backgroundColor = 'red'; } ``` 这段代码,我们通过this.$refs.myIframe获取到了id为"my-iframe"的元素,然后使用contentDocument获取到了iframe的文档对象。最后,我们将iframe的背景颜色设置为红色。 4. 最后,在Vue组件的模板,将changeIframeBgColor()方法绑定到一个按钮或其他事件上,以触发修改背景颜色的操作。例如: ```html <div id="my-iframe"> <iframe ref="myIframe" src="http://example.com"></iframe> <button @click="changeIframeBgColor">修改背景颜色</button> </div> ``` 在这个例子,我们在按钮上添加了点击事件,点击按钮就会触发changeIframeBgColor()方法,从而修改iframe的背景颜色为红色。 请注意,为了获得正确的元素引用,需要确保iframe的加载是同步的,并且iframeVue组件在同一个域。如果跨域,可能会遇到安全限制。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值