简体   繁体   English

对象不支持IE9中的属性或方法'append'

[英]Object doesn't support property or method 'append' in IE9

this script is working in firefox or chrome but only gets half way in IE9 which is the top browser for our websites. 这个脚本在firefox或chrome中工作,但只能在IE9中获得一半,IE9是我们网站的顶级浏览器。

the problem im getting is its throwing this error. 我得到的问题是它抛出这个错误。

SCRIPT438: Object doesn't support property or method 'append' calc_ajax.js, line 26 character 21 SCRIPT438:对象不支持属性或方法'append'calc_ajax.js,第26行字符21

on this line: item.append(link); 在这一行:item.append(link);

and im stuck why. 并且我坚持了原因。 any help would be appreciated. 任何帮助,将不胜感激。

$(document).ready(function(){
$('.first a.btn').click(function(){
    $('.first a.active').removeClass('active');
    $(this).addClass('active');
    $('.second .title').addClass('active');

    var id = $(this).data('cat-id');

    var wrap = $('<div>');

    $.ajax({
        url:    script_url,
        type:   "post",
        data: {"cat": id},
        dataType: "json"
    }).success(function(result){
        if(result.status == "ok"){
            $.each(result.data, function(i, elem){
                item = $("<div class='body-area'>").append($("<img src='" + elem.image + "'>"));
                link = $("<a href='#results' class='btn'>");
                link.text(elem.name);
                link.data('subcat-id', elem.id);
                item.append(link);

                wrap.append(item);

            });
            $('.second .body').html(wrap).slideDown('fast');
        }
    });
});

$('.second a.btn').live('click', function(){
    $('.second .body-area.active').removeClass('active');
    $(this).parent().addClass('active');

    var sub_id = $(this).data('subcat-id');        

    $.ajax({
        url:    script_url,
        type:   "post",
        data: {"subcat": sub_id},
        dataType: "json"
    }).success(function(result){
        if(result.status == "ok"){
            $('.third .title').text(result.data.title);
            $('.third .body').html(result.data.body);
            $('.third').slideDown('fast'); 
        }
    });        
});

}); });

window.item is a special method in Internet Explorer and since the code you pasted wasn't declaring a local variable item it was trying to reassign a native method in IE. window.item是Internet Explorer中的一种特殊方法,由于您粘贴的代码未声明本地变量item因此它尝试在IE中重新分配本机方法。 IE didn't allows the reassignment to happen so you didn't really get the jQuery object you were expecting in the item variable and therefore the append method isn't available. IE不允许重新分配,因此您没有真正获得item变量中您期望的jQuery对象,因此append方法不可用。

The easiest way to fix the code is to add a var right before you use the item variable. 修复代码的最简单方法是在使用item变量之前添加var I've thrown together a jsFiddle showing how it fixes the issue in IE http://jsfiddle.net/httyY/ 我把一个jsFiddle放在一起,展示它如何修复IE中的问题http://jsfiddle.net/httyY/

$.ajax({
    url:    script_url,
    type:   "post",
    data: {"cat": id},
    dataType: "json"
}).success(function(result){
    if(result.status == "ok"){
        $.each(result.data, function(i, elem){
            var item = $("<div class='body-area'>").append($("<img src='" + elem.image + "'>"));
            var link = $("<a href='#results' class='btn'>");
            link.text(elem.name);
            link.data('subcat-id', elem.id);
            item.append(link);

            wrap.append(item);

        });
        $('.second .body').html(wrap).slideDown('fast');
    }
});

I got the same error on IE11 when using the native function document.body.append . 当使用本机函数document.body.append时,我在IE11上遇到了同样的错误。

You can either use document.body.appendChild or insert the polyfill from MDN ( https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append#Polyfill ). 您可以使用document.body.appendChild或从MDN插入polyfill( https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append#Polyfill )。

try: 尝试:

$.each(result.data, function(i, elem){
   var item = $("<div class='body-area'>").append($("<img src='" + elem.image + "'>"));
   var link = $("<a />", {"href" :"#results", "class": "btn"});
   link.text(elem.name);
   link.data('subcat-id', elem.id);
   item.append(link);
   wrap.append(item);    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IE9错误:对象不支持属性或方法“ setAttribute” - IE9 error: Object doesn't support property or method 'setAttribute' 对象不支持此属性或方法 -- 在 IE9 中调用 Applet - Object doesn't support this property or method -- Calling an Applet in IE9 IE9:对象不支持属性“是” - IE9: Object doesn't support the property “is” TypeError:对象在IE8 / IE9中不支持此属性或方法 - TypeError: Object doesn't support this property or method in IE8 / IE9 对象不支持IE9和IE8中的属性或方法“切换” - Object doesn't support property or method 'Toggle' in IE9 and IE8 ExtJs4 + IE9 = Object不支持属性或方法&#39;createContextualFragment&#39; - ExtJs4 + IE9 = Object doesn't support property or method 'createContextualFragment' SCRIPT438:对象不支持属性或方法“创建”-IE9 - SCRIPT438: Object doesn't support property or method 'create' - IE9 Angular 4 IE9文件上传Script438:对象不支持属性或方法“应用” - Angular 4 IE9 file upload Script438:Object doesn't support property or method 'apply' Fineuploader IE9 CORS iframe错误“对象不支持属性或方法&#39;_parseJsonResponse&#39; - Fineuploader IE9 CORS iframe error "object doesn't support property or method '_parseJsonResponse' IE9中的新XMLHttpRequest导致JScript运行时错误:对象不支持此属性或方法 - new XMLHttpRequest in IE9 causes a JScript runtime error: Object doesn't support this property or method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM