Home > Article > Web Front-end > Example of how to use iframe elements in vue components
This article mainly introduces the sample code for using iframe elements in vue components. The editor thinks it is quite good. Now I will share it with you and give you a reference. I hope it can help everyone.
You need to display the hyperlink in the vue component on this page, and the address bar does not change:
<template> <p class="accept-container"> <p class="go-back" v-show="goBackState" @click="goBack">GoBack</p> <ul> <li v-for="item in webAddress"> <a :href="item.link" rel="external nofollow" target="showHere" @click="showIframe">{{item.name}}</a> </li> </ul> <iframe v-show="iframeState" id="show-iframe" frameborder=0 name="showHere" scrolling=auto src=""></iframe> </p> </template> <script> export default { name: 'hello', data () { return { iframeState:false, goBackState:false, webAddress: [ { name:'segmentFault', link:'https://segmentfault.com/a/1190000004502619' }, { name:'博客', link:'http://vuex.vuejs.org/' }, { name:'特效', link:'http://www.yyyweb.com/377.html' } ] } }, mounted(){ const oIframe = document.getElementById('show-iframe'); const deviceWidth = document.documentElement.clientWidth; const deviceHeight = document.documentElement.clientHeight; oIframe.style.width = deviceWidth + 'px'; oIframe.style.height = deviceHeight + 'px'; }, methods:{ goBack(){ this.goBackState = false; this.iframeState = false; }, showIframe(){ this.goBackState = true; this.iframeState = true; } } } </script> <style scoped> </style>
If you need to prevent the elements of the same layer from being overwritten, you can add
Copy code The code is as follows:
5ec7514b26d163fd1bdd6aa793040878065276f04003e4622c4fe6b64f465b88
But html5 has a new dialog element for dialogue frame.
Some methods of iframe:
Get iframe content:
var iframe = document.getElementById("iframe1"); var iwindow = iframe.contentWindow; var idoc = iwindow.document; 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
Adaptive iframe:
That is, 1 remove the scroll bar, 2 set the width and height
var iwindow = iframe.contentWindow; var idoc = iwindow.document; iframe.height = idoc.body.offsetHeight;
Example:
<iframe id="google_ads_frame2" name="google_ads_frame2" width="160" height="600" frameborder="0" src="target.html" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true"> </iframe>
Related recommendations:
Detailed explanation of the three writing forms of vue components
How to publish vue components To npm
vue2.0 data two-way binding with form bootstrap+vue component
The above is the detailed content of Example of how to use iframe elements in vue components. For more information, please follow other related articles on the PHP Chinese website!