JS动态添加iframe的代码_javascript技巧

WBOY
Release: 2016-05-16 15:39:36
Original
1689 people have browsed it

一般对于ie9以上的浏览器、chrome等浏览器都可以支持

var iframe = document.createElement('iframe'); iframe.src="http://www.jb51.net"; document.body.appendChild(iframe);
Copy after login

但考虑浏览器的兼容性问题可以使用下面的代码

try{ var iframe = document.createElement(''); }catch(e){ var iframe = document.createElement('iframe'); iframe.name = 'ifr'; }
Copy after login

或者

if(ie && version < 9) { var iframe = document.createElement(''); } else { var iframe = document.createElement('iframe'); iframe.setAttribute('src','http://www.jb51.net'); }
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!