弹出点击未打开未捕获TypeError:无法读取属性' top'未定义的

时间:2017-07-27 05:29:50

标签: javascript jquery

我遇到谷歌浏览器返回的情况

  

未捕获的TypeError:无法读取属性' top'未定义的

这是我的jquery代码

$(document).on('click', '.show_popup', function(){
    document.body.style.overflow = "hidden";
    $('body').css('overflow', 'hidden');    
    callAjax($(this).attr('href'), 'mode=ajax', function(t){
        $('.popup-screen').html(t);
        $('.main-overlay').show();

        // $('').raty();
        $('#star').raty({
          click: function(score, evt) {
            $('#review_rating').val( score);
          }
        });
        // window.scroll(0,0);
        $("html, body").animate({ scrollTop: $(".screenTable").offset().top },500);
    });
    return false;
});

请帮助,并在

后出错
  

XMLHttpRequest.self.xmlHttpReq.onreadystatechange

1 个答案:

答案 0 :(得分:2)

此错误显示,因为无法找到类.screenTable的元素。

您可以在调用top之前检查元素是否存在以防止错误

    var STable = $('.screenTable');
    if (STable.length) //1
    { 
      $("html, body").animate({ scrollTop: $(".screenTable").offset().top },500);
      alert("top");
    }
    else{
    alert("element not found, so no top");
    }

始终将您的功能包裹在$( document ).ready() More info on document.ready

$( document ).ready(function() {

//All elements has been loaded and are available.

});