vue 给iframe设置src_vue 中引入iframe,动态设置其src,遇到的一些小问题总结

iframe加载与事件处理优化
博客探讨了在Vue中处理iframe加载的问题,包括如何去除边框、滚动条,以及解决加载完成后onload事件不触发和页面无响应的状况。通过设置定时器确保iframe元素可用并绑定onload事件,确保正确加载内容。同时强调了iframe源代码的位置对加载成功的重要性。

1.重置其样式,去掉外框以及滚动条等

id="myIframe"

ref="iframe_a"

:src="mySrc"

width="100%"

height="100%"

frameborder="no"

border="0"

marginwidth="0"

marginheight="0"

scrolling="no"

allowtransparency="yes"

/>

2.在mounted里也找不到iframe 的dom元素,于是用了很笨的办法去处理,希望路过的大佬可以指点一下,有好的办法教教我好吗??

export default {

data() {

return {

mySrc: '',

loading: true, // 如果正在加载,就会盖住展示内容

timer: null, // 第1个定时器

timer1: null // 第2个定时器

}

},

mounted() {

if (this.$refs && this.$refs['iframe_a']) {

this.setLoading(this.$refs['iframe_a'])

} else {

// console.log('延时加载第一次')

clearTimeout(this.timer1)

this.timer1 = setTimeout(() => {

this.setLoading(this.$refs['iframe_a'])

}, 500)

}

},

destroyed() {

this.timer = null

this.timer1 = null

},

methods: {

// 补坑函数1

setLoading(iframeDom) {

if (iframeDom) {

// console.log('找到元素了,执行iframeDom.onload')

const iframeD = document.getElementById('myIframeWebgl')

iframeD.onload = () => {

// console.log('加载完了-----------')

this.loading = false

}

iframeD.src = this.setRoute()

} else {

this.setTim()

}

},

// 补坑函数2

setTim() {

const iframeD = document.getElementById('myIframe')

clearTimeout(this.timer)

this.timer = setTimeout(() => {

// console.log('延时加载第----次')

this.setLoading(iframeD)

}, 200);

},

}

}

3.在此之前,加载完成的onload事件有时候不会触发,点击页面有时候没有反应,地址改了但是页面没有变动,于是解决如下:

问题在于: iframe的绑定地址的代码一定要放在onload下面,要不然就完了

如果使用原生来写,大概这样子

let myIframeDiv= document.querySelector('#myIframeDiv')

let iframe = document.createElement('iframe')

iframe.onload = () => {

console.log('这样子就没问题了')

}

iframe.src = this.src // 就是这一行代码,一定要注意哦

myIframeDiv.appendChild(iframe)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值