【问题标题】:Copy Content from Iframe to div将内容从 iframe 复制到 div
【发布时间】:2018-10-13 17:32:15
【问题描述】:

我想从我的 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;
        };

【问题讨论】:

    标签: javascript jquery asp.net iframe webforms


    【解决方案1】:

    记住!您的 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 */ }
    

    【讨论】:

      猜你喜欢
      • 2011-01-07
      • 2017-04-07
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 2011-05-18
      相关资源
      最近更新 更多