admin管理员组文章数量:1355540
Anyone can tell me why i am getting this error:
Uncaught TypeError: Cannot read property 'replace' of undefined
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();
Anyone can tell me why i am getting this error:
Uncaught TypeError: Cannot read property 'replace' of undefined
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();
Share
Improve this question
asked Aug 26, 2015 at 16:00
user4082764user4082764
3
-
3
It just means that
pid
isundefined
. – Pointy Commented Aug 26, 2015 at 16:02 -
If you can use a fallback (e.g. the empty string), you can use the
||
operator:var pid = $('#NewPostbody').attr('class') || '';
. – Sebastian Simon Commented Aug 26, 2015 at 16:15 -
1
Would you be opposed if I edited your title and tags? This honestly doesn't have anything to do with jQuery. It's a standard JavaScript error message caused by an
undefined
value... – War10ck Commented Aug 26, 2015 at 16:18
1 Answer
Reset to default 2I believe that this error is caused by one of two scenarios, based on the given information above:
$('#NewPostBody)
is not being found in the DOM$('#NewPostBody)
is being found but has no class attribute.
This can be solved using the following method:
var pid = ($('#NewPostBody').length && $('#NewPostBody').attr('class'))
? $('#NewPostBody').attr('class')
: "";
The ternary operator along with truthy/falsy logic should result in either the class being returned or an empty string. Either way, pid.replace('post-t', '')
can safely be called without resulting in an error.
本文标签: javascriptJquery Uncaught TypeError Cannot read property 39replace39 of undefinedStack Overflow
版权声明:本文标题:javascript - Jquery Uncaught TypeError: Cannot read property 'replace' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743938380a2565105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论