通过JavaScript将IFRAME添加到DOM [英] Adding IFRAME to DOM by Javascript

查看:178
本文介绍了通过JavaScript将IFRAME添加到DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面中添加 iframe iframe 应该引用一个URL。我添加了以下代码到页面HTML,但它不起作用:

I want to add an iframe to the page. This iframe should refer to a URL. I added the below code to page HTML, but it doesn't work:

document.createElement('<iframe src='http://example.com'></iframe>');


推荐答案

你去:

var iframe;

iframe = document.createElement('iframe');
iframe.src = 'http://example.com/file.zip';
iframe.style.display = 'none';
document.body.appendChild(iframe);

现场演示: http://jsfiddle.net/USSXF/2/

您的代码无效因为您将整个HTML字符串传递到 createElement 函数(jQuery style:))中,这是无效。此函数的有效参数是表示标签名称的字符串(如'div''iframe' 'p'等)。

Your code doesn't work because you're passing an entire HTML string into the createElement function ("jQuery style" :)), which is invalid. The valid parameter for this function is a string representing the tag-name (like 'div', 'iframe', 'p', etc.).

阅读关于 document.createElement 这里

这篇关于通过JavaScript将IFRAME添加到DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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