我想从index.aspx页面上的特定Iframe复制内容,然后打开一个新窗口并将其粘贴到div中。
我现在得到的是下面的代码(不是我的代码),但我希望它不那么混乱。就像这个:
$('#ifContent').clone().appendTo(w.document.getElementById('iFrameDiv')
有人能帮帮我吗
var objContent = getContentWindow();
if (objContent.length > 0) {
var w = window.open();
var path = window.location.href.toString().toLowerCase();
w.onerror = function () { alert('Error'); }
w.document.open();
w.document.writeln('<html>');
w.document.writeln('<head><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">');
w.document.writeln('<title>MyTitel</title>');
w.document.writeln('<base href="' + path.replace("index.aspx", "/test") + '" />');
w.document.writeln('<script type="text/javascript" src="javascript/jquery.min.js"></' + 'script>');
w.document.writeln('</head>');
w.document.writeln('<body>');
objContent.each(function () {
var objContainer = $('frame', this.contentDocument);
if (objContainer.length < 1)
{ objContainer = $(this); }
objContainer.each(function () {
$('body', this.contentDocument).children('form').children().each(function () {
var strHTML = $(this).html().toString().trim().toLowerCase();
if ((strHTML != '') && (strHTML.indexOf('cdata') < 0) && (strHTML.indexOf('viewstate') < 0) && (strHTML.indexOf('please wait') < 0) && (strHTML.indexOf('top') < 0)) {
if (this.nodeName.toString().toLowerCase() != 'script') {
w.document.writeln('<' + this.nodeName + ' class="' + $(this).attr('class') + '" style="' + $(this).attr('style') + '">');
w.document.writeln($(this).html().toString().replace(new RegExp('onclick', 'g'), 'oncclick').replace(new RegExp('href', 'g'), 'hhref').replace(new RegExp('onchange', 'g'), 'oncchange'));
w.document.writeln('</' + this.nodeName + '>');
}
}
});
});
});
w.document.writeln('</body>');
w.document.close();
w.focus();
w.print();
}
else {
top.focus();
top.print();
};
编辑:所以我试着这样做,就像下面Pop Alexandru建议的那样,但是我的“内容”变量是空的。
var w = window.open();
w.document.writeln('<html>');
w.document.writeln('<head><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">');
w.document.writeln('<script type="text/javascript" src="javascript/jquery.min.js"></' + 'script>');
w.document.writeln('</head>');
w.document.writeln('<body>');
w.document.writeln('<div id="iFrameDiv"> </div>')
var iframe = document.getElementById('ifContent');
var iframeContent = iframe.contentWindow.document;
//After this line my content is null
var content = document.getElementById('iFrameDiv');
// set contents of this div to iframe's content
content.innerHTML = iframeContent.innerHTML;
w.document.writeln('</body>');
w.document.close();
w.focus();
编辑2:
所以现在它的作品:
w.onload = function () {
var iframe = document.getElementById('ifContent');
var content = w.document.getElementById('iFrameDiv');
content.innerHTML = iframe.contentDocument.body.innerHTML;
};
1条答案
按热度按时间qrjkbowd1#
记住!你的iframe必须和你的页面在同一个域名上