javascript填充iframe,一次修改源代码
使用相同的HTML预览填充2个iFrame的内容。尽管对于其中一个iFrame,我需要通过删除图像来修改data/htmljavascript填充iframe,一次修改源代码,javascript,jquery,iframe,Javascript,Jquery,Iframe,使用相同的HTML预览填充2个iFrame的内容。尽管对于其中一个iFrame,我需要通过删除图像来修改data/html $.ajax({ url: '', dataType: 'text', success: function(data) { if(data){ var iframe = document.getElementById('preview'); iframe.parentNode.style
$.ajax({
url: '',
dataType: 'text',
success: function(data) {
if(data){
var iframe = document.getElementById('preview');
iframe.parentNode.style.display = "block";
iframe = (iframe.contentWindow) ? iframe.contentWindow : (iframe.contentDocument.document) ? iframe.contentDocument.document : iframe.contentDocument;
iframe.document.open();
iframe.document.write(data);
iframe.document.close();
var iframe = document.getElementById('preview-noimgs');
iframe.parentNode.style.display = "block";
iframe = (iframe.contentWindow) ? iframe.contentWindow : (iframe.contentDocument.document) ? iframe.contentDocument.document : iframe.contentDocument;
iframe.document.open();
var $data = $(data);
$data.find("img").removeAttr("src");
iframe.document.write($data.html());
iframe.document.close();
}
}
});
这给了我:NULL
或[object object]
,这取决于我是否添加了.html()
修改源的正确方法是什么
阅读jquery文档时,我发现parseHTML()
,但遗憾的是,由于我们仅限于1.7.1版,因此无法使用它。还有其他方法吗
预览:请注意,.html()
返回内部html,而不是外部html
我想你应该试试这个:
var $data = $("<div>" + data + "</div>");
我的数据是带有doctype的完整HTML,这可能是jquery出现问题的原因吗?我想我需要确保在写入iframe之前修改数据。Jquery在过去修改iframe时遇到过问题。我只是有点不知道下一步该做什么。@John Magnolia:一个错误,应该是
$(iframe).contents().find(“html”).find(“img”).removeAttr(“src”)代码>。我更新了答案我认为在iFrame中处理内容可能会有限制,您可以使用php或ajax等服务将原始页面下载为文本,去除img标记等,然后显示html。
iframe = document.getElementById('preview-noimgs');
iframe.parentNode.style.display = "block";
var iframeWindow = (iframe.contentWindow) ? iframe.contentWindow : (iframe.contentDocument.document) ? iframe.contentDocument.document : iframe.contentDocument;
iframeWindow.document.open();
iframeWindow.document.write(data);
iframeWindow.document.close();
$(iframe).contents().find("html").find("img").removeAttr("src"); //remove the `src`