任何人都可以告诉我为什么我收到此错误:
未捕获的TypeError:无法读取未定义的属性’replace’
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();
最佳答案
我认为这个错误是由两种情况中的一种引起的,基于上面给出的信息:
@H_502_29@> $(‘#NewPostBody)在DOM中找不到
> $(‘#NewPostBody)正在被找到但没有class属性.
var pid = ($('#NewPostBody').length && $('#NewPostBody').attr('class'))
? $('#NewPostBody').attr('class')
: "";
三元运算符以及truthy / falsy逻辑应该导致返回的类或空字符串.无论哪种方式,都可以安全地调用pid.replace(‘post-t’,”)而不会导致错误.