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

时间:2011-07-29 01:25:36

标签: jquery internet-explorer-8 append appendto

我使用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);

}

jquery中发生错误(this.appendChild(elem);)

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

2 个答案:

答案 0 :(得分:0)

不要在结尾处将“o”附加到目的地,而是在代码中尝试此操作。我认为这会奏效。

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

并评论此行

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

答案 1 :(得分:0)

只需使用jquery的.html()函数即可。 用于填充<ul id="List"></ul>,例如:

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