【发布时间】:2015-08-26 16:00:49
【问题描述】:
谁能告诉我为什么会出现这个错误:
未捕获的类型错误:无法读取未定义的“替换”属性
function checkNewPost(x) {
var pid = $('#NewPostbody').attr('class');
if(pid === 'post-t') {
setTimeout(checkNewPost, <?php echo $p_id; ?>);
} else {
$.ajax({
type: "POST",
url: "/html_postReply.php",
data: "pid="+pid.replace('post-t', '')+"&type=1",
success: function(html) {
if(html) {
$('.tekin').append(html);
jQuery("span.timeago").timeago();
$(".tekin").scrollTop($(".tekin")[0].scrollHeight);
}
if(!x) {
setTimeout(checkNewPost, <?php echo $p_id; ?>);
}
}
});
}
}
checkNewPost();
【问题讨论】:
-
这只是表示
pid是undefined。 -
如果您可以使用回退(例如空字符串),您可以使用
||运算符:var pid = $('#NewPostbody').attr('class') || '';。 -
如果我编辑了你的标题和标签,你会反对吗?老实说,这与 jQuery 没有任何关系。这是由
undefined值引起的标准 JavaScript 错误消息...
标签: javascript jquery ajax