问题描述
当尝试将克隆的内容附加到新窗口中时,Microsoft Edge抛出不支持此接口"错误.这是一个示例:
Microsoft Edge is throwing 'No such interface supported' error when trying to append cloned content into new window. Here is an example:
jQuery(document).ready(function() {
jQuery('.trigger').click(function() {
var target = jQuery(this).data('print_target');
var w = window.open('', '', 'status=no, toolbar=no, menubar=no, location=no');
var print_html = '<!DOCTYPE html><head><title>' + document.getElementsByTagName('title')[0].innerHTML + '</title></head><body></body></html>';
w.document.write( print_html );
var ua = window.navigator.userAgent;
var ie = true;
//.html() required for IE browsers
if ( ua.indexOf("MSIE ") != -1) {
//console.log('MSIE - Craptastic');
jQuery(w.document.body).append( jQuery( target ).clone( true ).html() );
}
else if ( ua.indexOf("Trident/") != -1) {
//console.log('IE 11 - Trident');
jQuery(w.document.body).append( jQuery( target ).clone( true ).html() );
}
else if ( ua.indexOf("Edge/") != -1 ){
console.log('IE 12 - Edge');
//error: No such interface supported
jQuery(w.document.body).append( jQuery( target ).clone( true ).html() );
//works
//jQuery(w.document.body).append( 'hey dude, this is some text' );
//works
//jQuery(w.document.body).html( jQuery( target ).clone( true ).html() );
}
else{
//console.log('proper browser');
jQuery(w.document.body).append( jQuery( target ).clone( true ) );
ie = false;
}
});
});
这只是Microsoft Edge的问题,它可在所有基于标准的浏览器以及IE浏览器7,8,9,10和11中使用.类似的问题也已在此线程中引发但未解决.
This is only an issue with Microsoft Edge, it works in all standards based browsers and IE browsers 7,8,9,10 and 11. A similar issue has been raised in this thread but not resolved.
这是一个显示内容的jsfiddle: https://jsfiddle.net/switzerbaden/nhtywLsp/
here is a jsfiddle showing what's what: https://jsfiddle.net/switzerbaden/nhtywLsp/
推荐答案
首先克隆HTML,然后在div之前添加div,然后在其之后关闭:
First clone the HTML, then add div before and close it after:
var new_target = jQuery.trim( jQuery( target ).clone( true ).html() );
jQuery( w.document.body ).append( "<div>" + new_target + "</div>" );
这篇关于jQuery append()到新窗口中无法与Microsoft Edge一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!