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 is undefined. – 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
Add a ment  | 

1 Answer 1

Reset to default 2

I believe that this error is caused by one of two scenarios, based on the given information above:

  1. $('#NewPostBody) is not being found in the DOM

  2. $('#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