jQuery append() 进入新窗口不适用于 Microsoft Edge [英] jQuery append() into new window not working with Microsoft Edge

查看:28
本文介绍了jQuery append() 进入新窗口不适用于 Microsoft Edge的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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/

推荐答案

先clone HTML,然后在之前添加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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆