用javascript附加到div创建iframe [英] Create iframe with javascript appending to a div

查看:79
本文介绍了用javascript附加到div创建iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用常规的javascript将iframe加载/附加到div中。我可以用JQuery做到这一点,但没有问题,但我不想包含js文件。我不断收到错误'document.getElementById(ad54)'(或任何我分配div的ID)。我有以下代码:

I want to load/append the iframe into a div with regular javascript. I can do this with JQuery without a problem, but I dont want to include the js file. I keep getting the error 'document.getElementById("ad54")' (or whatever id I assign the div). I have the following code:

var link = "http://www.google.com"
var iframe = document.createElement('iframe');
iframe.frameBorder=0;
iframe.width="300px";
iframe.height="250px";
iframe.id="randomid";
iframe.setAttribute("src", link);
document.getElementById("ad54").appendChild(iframe);

<div id="ad54"></div>


推荐答案

p>

You should write this inside window.onload like

window.onload = function(){
   var link = "http://www.quirksmode.org/iframetest2.html"
var iframe = document.createElement('iframe');
iframe.frameBorder=0;
iframe.width="300px";
iframe.height="250px";
iframe.id="randomid";
iframe.setAttribute("src", link);
document.getElementById("ad54").appendChild(iframe);

}

这篇关于用javascript附加到div创建iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆