jquery iframe html() - 我可以访问iframe,但无法获取其内容

时间:2014-03-24 19:03:13

标签: jquery iframe

这已经多次讨论过,但我无法使其发挥作用。

我在运行中创建了一个iframe,我可以访问其属性,如宽度或CSS样式,但无法访问其innerHTML。

这是我的代码,我在这里缺少什么?

谢谢!

var iframe = document.createElement('iframe');
iframe.frameBorder = 0;
iframe.width = '100%';
iframe.height = '180px';
iframe.src  = 'javascript:void(0);';
iframe.id = '_input_'+que_evento_id;
$(iframe).css('margin-top', '5px');
$(iframe).css('background-color', '#ffffff');
$(iframe).css('border', 'solid 1px #ff7223');
var conteudo = $('#span_'+que_evento_id+'').text();
var doc = '<html><head><meta http-equiv=\'Content-Type\' content=\'text/html; charset=UTF-8\'><title>Editar</title></head><body style=\'padding:5px\'>'+conteudo+'</body></html>';
iframe_doc = iframe.contentWindow.document;
try {
    iframe_doc.designMode = 'on';
} catch ( e ) {
    $(iframe_doc).focus(function() { iframe_doc.designMode(); } );
}
iframe_doc.open();
iframe_doc.write(doc);
iframe_doc.close();

width = $('#_input_'+que_evento_id+'').attr('width');
console.log(width); // returns 100% wich is correct
o_que_vamos_editar = $('#_input_'+que_evento_id+'').contents().find('body').html();
console.log('log '+o_que_vamos_editar); // returns "log "

0 个答案:

没有答案
相关问题