通过Javascript将IFRAME添加到DOM

Xaq*_*ron 7 javascript asp.net iframe download

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

document.createElement('<iframe src='http://example.com'></iframe>');
Run Code Online (Sandbox Code Playgroud)

Šim*_*das 15

干得好:

var iframe;

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

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

您的代码不起作用,因为您将整个HTML字符串传递给createElement函数("jQuery样式" :)),这是无效的.此功能的有效参数是代表标签的名称(如一个字符串'div','iframe','p',等).

document.createElement这里阅读.