[英]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.