【发布时间】:2011-09-13 06:05:29
【问题描述】:
我想在页面上添加一个iframe。这个iframe 应该引用一个URL。我将以下代码添加到页面 HTML,但它不起作用:
document.createElement('<iframe src='http://example.com'></iframe>');
【问题讨论】:
标签: javascript asp.net iframe download
我想在页面上添加一个iframe。这个iframe 应该引用一个URL。我将以下代码添加到页面 HTML,但它不起作用:
document.createElement('<iframe src='http://example.com'></iframe>');
【问题讨论】:
标签: javascript asp.net iframe download
给你:
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 样式”:))。此函数的有效参数是表示标记名称的字符串(如'div'、'iframe'、'p' 等)。
【讨论】:
iframe? (页面按钮加了一个大框)
您需要使用 document.appendChild 将节点附加到 DOM
您还需要转义内部单引号或使用双引号。
【讨论】:
document.write(unescape('%3Ciframe src="//example.com/file.zip"%3E%3C/iframe%3E')
【讨论】: