2016-07-26 94 views
2

當試圖將克隆內容附加到新窗口時,Microsoft Edge會拋出'No such interface supported'錯誤。這裏有一個例子:jQuery追加()到新窗口不能與Microsoft Edge一起工作

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

這僅僅是與微軟邊緣的問題,它適用於所有基於標準的瀏覽器和IE瀏覽器7,8,9,10和11類似的問題一直raised in this thread,但沒有得到解決。

這裏是展示什麼是什麼的jsfiddle:https://jsfiddle.net/switzerbaden/nhtywLsp/

回答

2

經過進一步調查,我發現,當你的HTML字符串產生多個頂級同級元素jQuery將創建一個DocumentFragment的。當Microsoft Edge嘗試將documentFragment追加到第二個窗口時,問題就出現了。

現在,我鼓勵您將HTML傳遞給append方法,該方法只產生一個頂級元素,並從兩側進行了空白裁剪。這樣,我們保證jQuery不會嘗試附加一個documentFragment,而是一個單獨的元素。

Microsoft Edge發生了一個錯誤,並且jQuery團隊已收到通知。


我是Microsoft Edge團隊的一名工程師,我看到了您指的問題。我要提交的bug,並作進一步調查,但暫時,一個解決辦法是隻有一個子元素在你的目標,並修剪內容時附加:

<div id="my_target"> 
    <div> 
     <!-- move contents in here --> 
    </div> 
</div> 
$(w.document.body).append(
    // Or, "<div>" + $(target).html() + "</div>" 
    $.trim($(target).clone(true).html()) 
); 

經過一些粗略的測試後,這似乎可以解決問題。