编辑
2019-03-22
建站知识
00

**说明:**使iframe加载拒绝被嵌套的页面

在使用iframe时,某些站点无法被载入,只能显示出空白页面 如使用以下方式载入百度时:

html
<body> <iframe id = 'iframe' src = 'https://www.baidu.com'></iframe> </body>

正确方式:

html
<body> <iframe id = 'iframe'></iframe> <script> var iframe = document.getElementById('iframe'); iframe.onload = function () { if (this.getAttribute('is-reset') == '1') { this.style.opacity = 0; this.style.display = 'block'; var url = this.getAttribute('src-url'); this.setAttribute('src', url); this.setAttribute('is-reset',0); } else { this.style.opacity = '1'; } }; window.onload = function(){ changeUrl('https://www.baidu.com'); } function changeUrl(url) { iframe.style.display = 'none'; iframe.setAttribute('is-reset',1); iframe.setAttribute('src', 'about:blank'); iframe.setAttribute('src-url', url); }; </script> </body>
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:7

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 转载请注明出处 许可协议。转载请注明出处!