簡體   English   中英

將內容從iframe復制到div

[英]Copy Content from Iframe to div

我想從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建議的那樣,但是我的“內容”變量為null。

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();

EDIT2:

所以現在它的作品:

w.onload = function () {
            var iframe = document.getElementById('ifContent');
            var content = w.document.getElementById('iFrameDiv');
            content.innerHTML = iframe.contentDocument.body.innerHTML;
        };

記住! 您的iframe必須與您的頁面位於同一域中

// Get your iframe element

var iframe = document.getElementById('iframe');

// Access contents of it like this

var iframeContent = iframe.contentWindow.document;

// Get your div element

var content = document.getElementById('source');

// set contents of this div to iframe's content

content.innerHTML = iframeContent.innerHTML;

// do something when iframe is onload 

iframe.onload = function() { /* put the above code here */ }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM