jQuery 的appendto 和append 在IE8 中不起作用?

发布于 2024-11-27 01:08:28 字数 6186 浏览 0 评论 0原文

我在使用 jQuery 的append 和appendTo 函数时遇到了一个奇怪的问题。我正在运行一个对象并根据对象属性创建元素。在我的函数中的一些地方,我使用 .append() 和 .appendTo() 方法非常好。然而在 IE8 和 IE7 中我遇到了一个奇怪的错误。当我注释掉以下行时,错误消失了: o.append(em)

有什么想法吗?

embedWindowsMedia: function(url, destination, opt){

        // Default MediaPlayer options:
        var d = {
                id: 'mediaPlayer',
                width: '320',
                height: '285',
                classid:'CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95',
                codebase: 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
                standby: 'Loading Microsoft Windows Media Player components...',
                type: 'application/x-oleobject',
                params: { fileName : '',
                        animationatStart : 'true',
                        transparentatStart : 'true',
                        autoStart : 'false',
                        showControls : 'false',
                        loop : 'true',
                        windowlessVideo: 'true'
                },
                embed: {
                    type: 'application/x-mplayer2',
                    pluginspage: 'http://microsoft.com/windows/mediaplayer/en/download/',
                    id: 'mediaPlayer',
                    name: 'mediaPlayer',
                    displaysize: '4',
                    autosize:'-1',
                    bgcolor:'darkblue',
                    showdisplay: '0',
                    showstatusbar:'-1',
                    videoborder3d:'-1',
                    width: '320',
                    height: '285',
                    src: '',
                    autostart:'',
                    loop :'false'
                }
        };
        // Handle overwriting default options:
        if (!url)
            return false;
        else{

            d.params.fileName = url;
            d.embed.src = url;
        }

        alert("urls set");
        if (opt.embed){
            d.embed = $.extend(d.embed, opt.embed)
            delete opt.embed;
        }
        if (opt.params){
            d.params = $.extend(d.params, opt.params)
            delete opt.params;
        }       
        var n = $.extend(d,opt);
        // Create object with options and all:
        var o = $("<object/>");
        for (var k in n){
            if (typeof n[k] == 'string'){
                o.attr(k,n[k]);
            }
            else if(typeof n[k] == 'object'){
                if (k=='params'){
                    for( var p in n[k]){
                        var param = $('<param/>');
                        param.attr('name', p).attr('value', n[k][p]).appendTo(o);
                    }
                }
                else if (k=='embed'){
                    var em = $('<embed/>');
                    for( var e in n[k]){
                        em.attr(e, n[k][e]);
                    }
                    o.append(em);
                }
            }
        }
        // Insert object into container:        
        if(destination)
            o.appendTo(destination);
    }

使用以下代码也得到相同的错误:

embedWindowsMedia: function(url, destination, opt){

    // Default MediaPlayer options:
    var d = {
            id: 'mediaPlayer',
            width: '320',
            height: '285',
            classid:'CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95',
            codebase: 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
            standby: 'Loading Microsoft Windows Media Player components...',
            type: 'application/x-oleobject',
            params: { fileName : '',
                    animationatStart : 'true',
                    transparentatStart : 'true',
                    autoStart : 'false',
                    showControls : 'false',
                    loop : 'true',
                    windowlessVideo: 'true'
            },
            embed: {
                type: 'application/x-mplayer2',
                pluginspage: 'http://microsoft.com/windows/mediaplayer/en/download/',
                id: 'mediaPlayer',
                name: 'mediaPlayer',
                displaysize: '4',
                autosize:'-1',
                bgcolor:'darkblue',
                showdisplay: '0',
                showstatusbar:'-1',
                videoborder3d:'-1',
                width: '320',
                height: '285',
                src: '',
                autostart:'',
                loop :'false'
            }
    };
    // Handle overwriting default options:
    if (!url)
        return false;
    else{

        d.params.fileName = url;
        d.embed.src = url;
    }

    alert("urls set");
    if (opt.embed){
        d.embed = $.extend(d.embed, opt.embed)
        delete opt.embed;
    }
    if (opt.params){
        d.params = $.extend(d.params, opt.params)
        delete opt.params;
    }       
    var n = $.extend(d,opt);
    // Create object with options and all:
    var o = $("<object/>").appendTo(destination);
    for (var k in n){
        if (typeof n[k] == 'string'){
            o.attr(k,n[k]);
        }
        else if(typeof n[k] == 'object'){
            if (k=='params'){
                for( var p in n[k]){
                    var param = $('<param/>');
                    param.attr('name', p).attr('value', n[k][p]).appendTo(o);
                }
            }
            else if (k=='embed'){
                //var em = $('<embed/>');
                var em = "<embed ";
                for( var e in n[k]){
                    em += e+'="'+n[k][e]+'" ';
                    //em.attr(e, n[k][e]);
                }
                em+= "></embed>";
                //$("#mediaPlayer").append(em);
            }
        }
    }
    console.log(em);
    $("#mediaPlayer").html(em);

}

Error attempts here in jquery (this.appendChild( elem );)

append: function() {
    return this.domManip(arguments, true, function( elem ) {
        if ( this.nodeType === 1 ) {
            this.appendChild( elem );
        }
    });
},

I'm encountering a weird issue using jQuery's append and appendTo function. I am running through an object and creating elements based on the objects properties. A few places in my function, I use the .append() and .appendTo() methods perfectly fine. However in IE8 and IE7 i get a weird error. The error disappears when I comment out the line: o.append(em)

Any ideas?

embedWindowsMedia: function(url, destination, opt){

        // Default MediaPlayer options:
        var d = {
                id: 'mediaPlayer',
                width: '320',
                height: '285',
                classid:'CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95',
                codebase: 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
                standby: 'Loading Microsoft Windows Media Player components...',
                type: 'application/x-oleobject',
                params: { fileName : '',
                        animationatStart : 'true',
                        transparentatStart : 'true',
                        autoStart : 'false',
                        showControls : 'false',
                        loop : 'true',
                        windowlessVideo: 'true'
                },
                embed: {
                    type: 'application/x-mplayer2',
                    pluginspage: 'http://microsoft.com/windows/mediaplayer/en/download/',
                    id: 'mediaPlayer',
                    name: 'mediaPlayer',
                    displaysize: '4',
                    autosize:'-1',
                    bgcolor:'darkblue',
                    showdisplay: '0',
                    showstatusbar:'-1',
                    videoborder3d:'-1',
                    width: '320',
                    height: '285',
                    src: '',
                    autostart:'',
                    loop :'false'
                }
        };
        // Handle overwriting default options:
        if (!url)
            return false;
        else{

            d.params.fileName = url;
            d.embed.src = url;
        }

        alert("urls set");
        if (opt.embed){
            d.embed = $.extend(d.embed, opt.embed)
            delete opt.embed;
        }
        if (opt.params){
            d.params = $.extend(d.params, opt.params)
            delete opt.params;
        }       
        var n = $.extend(d,opt);
        // Create object with options and all:
        var o = $("<object/>");
        for (var k in n){
            if (typeof n[k] == 'string'){
                o.attr(k,n[k]);
            }
            else if(typeof n[k] == 'object'){
                if (k=='params'){
                    for( var p in n[k]){
                        var param = $('<param/>');
                        param.attr('name', p).attr('value', n[k][p]).appendTo(o);
                    }
                }
                else if (k=='embed'){
                    var em = $('<embed/>');
                    for( var e in n[k]){
                        em.attr(e, n[k][e]);
                    }
                    o.append(em);
                }
            }
        }
        // Insert object into container:        
        if(destination)
            o.appendTo(destination);
    }

Getting the same exact error with the following code too:

embedWindowsMedia: function(url, destination, opt){

    // Default MediaPlayer options:
    var d = {
            id: 'mediaPlayer',
            width: '320',
            height: '285',
            classid:'CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95',
            codebase: 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
            standby: 'Loading Microsoft Windows Media Player components...',
            type: 'application/x-oleobject',
            params: { fileName : '',
                    animationatStart : 'true',
                    transparentatStart : 'true',
                    autoStart : 'false',
                    showControls : 'false',
                    loop : 'true',
                    windowlessVideo: 'true'
            },
            embed: {
                type: 'application/x-mplayer2',
                pluginspage: 'http://microsoft.com/windows/mediaplayer/en/download/',
                id: 'mediaPlayer',
                name: 'mediaPlayer',
                displaysize: '4',
                autosize:'-1',
                bgcolor:'darkblue',
                showdisplay: '0',
                showstatusbar:'-1',
                videoborder3d:'-1',
                width: '320',
                height: '285',
                src: '',
                autostart:'',
                loop :'false'
            }
    };
    // Handle overwriting default options:
    if (!url)
        return false;
    else{

        d.params.fileName = url;
        d.embed.src = url;
    }

    alert("urls set");
    if (opt.embed){
        d.embed = $.extend(d.embed, opt.embed)
        delete opt.embed;
    }
    if (opt.params){
        d.params = $.extend(d.params, opt.params)
        delete opt.params;
    }       
    var n = $.extend(d,opt);
    // Create object with options and all:
    var o = $("<object/>").appendTo(destination);
    for (var k in n){
        if (typeof n[k] == 'string'){
            o.attr(k,n[k]);
        }
        else if(typeof n[k] == 'object'){
            if (k=='params'){
                for( var p in n[k]){
                    var param = $('<param/>');
                    param.attr('name', p).attr('value', n[k][p]).appendTo(o);
                }
            }
            else if (k=='embed'){
                //var em = $('<embed/>');
                var em = "<embed ";
                for( var e in n[k]){
                    em += e+'="'+n[k][e]+'" ';
                    //em.attr(e, n[k][e]);
                }
                em+= "></embed>";
                //$("#mediaPlayer").append(em);
            }
        }
    }
    console.log(em);
    $("#mediaPlayer").html(em);

}

Error happens here in jquery (this.appendChild( elem );)

append: function() {
    return this.domManip(arguments, true, function( elem ) {
        if ( this.nodeType === 1 ) {
            this.appendChild( elem );
        }
    });
},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

前事休说 2024-12-04 01:08:28

不要在最后将“o”附加到目标中,而是在代码中尝试这样做。我认为这会起作用。

var o = $("<object/>").appendTo(destination);

并评论这一行

// Insert object into container:        
        if(destination)
            o.appendTo(destination);

Instead of appending "o" into destination at the end try this in your code. I think this will work.

var o = $("<object/>").appendTo(destination);

and comment this lines

// Insert object into container:        
        if(destination)
            o.appendTo(destination);
哥,最终变帅啦 2024-12-04 01:08:28

只需使用 jquery 的 .html() 函数即可。
用于填充

    ,例如:

    var ListItems = $("#List");
        ListItems.html('');
    var liString = "";
    //.forEach may not work in IE8, so use "for"
    for (var i= 0; i<SubjectsArray.length; i++)
    {
        var entry = SubjectsArray[i];
        liString += "<li>"+entry+"</li>";
    };
    ListItems.html(liString);
    

    Just use .html() function of jquery.
    For populating <ul id="List"></ul>, for example:

    var ListItems = $("#List");
        ListItems.html('');
    var liString = "";
    //.forEach may not work in IE8, so use "for"
    for (var i= 0; i<SubjectsArray.length; i++)
    {
        var entry = SubjectsArray[i];
        liString += "<li>"+entry+"</li>";
    };
    ListItems.html(liString);
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文