我通过jQuery的.data方法访问存储为对象的数组但由于某种原因我无法在IE9中获取嵌套属性
var $commentdata = $comment.data('comments'),
$position = $commentdata.playtime;
在其他所有浏览器中都能正常运行。但是在IE9中我收到以下错误: -
错误:无法获取属性“playtime”的值:object为null或undefined
知道为什么会这样,以及如何解决它?
编辑:这是将数据添加到dom的函数:
function getComments(oTable) {
var $array = [];
if (oTable.find('.dataTables_empty').length === 0) {
//build array of songid's to send to server
oTable.find('td').each(function () {
"use strict";
var $songdata = $(this).data('data'),
$songid = $songdata.songid,
$duration = $songdata.duration;
$array.push($songid);
});
$.ajax({
type: "GET",
dataType: "json",
async: false,
url: "/wp-content/comment_data.php",
data: 'songid=' + $array,
success: function (data) {
"use strict";
$commentarray.length = 0;
$commentarray.push(data);
//apply template to each song and render comment icons
console.log($commentarray);
$.each(data, function (key, value) {
var $songdata = data[key];
if ($songdata !== null) {
var $songid = $songdata[0].songid,
songid = 'songid' + $songid,
$commentbar = $('#comment-bar-' + $songid),
sID = $commentbar.closest('td').find('a.playable').data('playable').sID,
song = soundManager.getSoundById(sID);
$commentbar.html($.render(data[key], 'commentBarTemplate'));
$.each($songdata, function (index, value) {
var commentdata = $songdata[index],
position = commentdata.position,
cid = commentdata.id,
$templateDiv = $('#timestamped-comment-template');
//$comment = $('#comment-' + cid).find('a');
//$comment.data('comments', commentdata);
song.onPosition(position * 1000, function () {
if ($('#example').find('td').hasClass('comments-shown') === false && $templateDiv.hasClass('hover') === false && $('body').attr('looping') === '') {
showNewComment($comment);
}
});
});
}
});
}
});
}
else {
return false;
}
}
我对jQuery很陌生,我仍然不知所措,如果这段代码不是很好,那么道歉。但是无论所有的数据都被正确地放入DOM中,我说除了IE之外的每个浏览器都是可以接受的。
我刚才注意到......在访问没有任何回复的评论时,它似乎只会触发错误。奇怪!
以下是JSON返回的一小部分:
{ "songid22" : null,
"songid23" : [ { "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test",
"datasize" : "204",
"display_name" : "admin",
"id" : "5",
"playtime" : 27.450980392200002,
"position" : "56",
"posttime" : "2011-09-28 15:32:41",
"replies" : [ null ],
"songid" : "23",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test comment",
"datasize" : "204",
"display_name" : "admin",
"id" : "6",
"playtime" : 42.647058823499997,
"position" : "87",
"posttime" : "2011-09-28 16:38:37",
"replies" : [ null ],
"songid" : "23",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
}
],
"songid24" : [ { "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "comment at 0:48",
"datasize" : "267",
"display_name" : "admin",
"id" : "2",
"playtime" : 17.977528089900002,
"position" : "48",
"posttime" : "2011-09-28 14:38:41",
"replies" : [ { "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "haha reply",
"cid" : "2",
"display_name" : "admin",
"id" : "1",
"posttime" : "2011-09-28 15:14:56",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "next replty",
"cid" : "2",
"display_name" : "admin",
"id" : "2",
"posttime" : "2011-09-28 15:15:07",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "first reply",
"cid" : "2",
"display_name" : "admin",
"id" : "3",
"posttime" : "2011-09-28 15:15:15",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
}
],
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "asdasd",
"datasize" : "267",
"display_name" : "admin",
"id" : "3",
"playtime" : 0.74906367041199995,
"position" : "2",
"posttime" : "2011-09-28 14:43:11",
"replies" : [ { "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test reply",
"cid" : "3",
"display_name" : "admin",
"id" : "5",
"posttime" : "2011-10-06 14:35:23",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test reply",
"cid" : "3",
"display_name" : "admin",
"id" : "6",
"posttime" : "2011-10-07 14:48:21",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
}
],
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test",
"datasize" : "267",
"display_name" : "admin",
"id" : "10",
"playtime" : 36.329588014999999,
"position" : "97",
"posttime" : "2011-10-06 14:15:12",
"replies" : [ null ],
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
],
我的评论检索适用于所有具有更多“回复”嵌套的评论。如果replies = null则IE会触发错误。
EDIT2:通过在IE上使用firebug Lite进一步调查我可以看到,commentdata仍然是一个应该存在的对象,但是在它们的JSON结构中没有嵌套“回复”的ann注释中它是完全空的。因此,似乎数据没有正确连接到dom,并且错误必须是我上面的代码,而不是在没有回复的注释中正确连接数组。奇怪的是,它在所有其他浏览器上工作正常。
还有什么想法?
答案 0 :(得分:1)
问题解决了......信不信由你在我的模板中发现了一个缺失的结束标记。
令人震惊的是我错过了它,虽然有点令人惊讶的是它会像它那样抛出IE 9。
我为浪费人们的时间而道歉。
干杯