Uncaught TypeError: Cannot read property 'top' of undefined

前端 未结 6 2322
长情又很酷
长情又很酷 2020-11-30 23:06

I apologize if this question has already been answered. I\'ve tried searching for solutions but could not find any that suited my code. I\'m still new to jQuery.

I h

6条回答
  •  没有蜡笔的小新
    2020-12-01 00:00

    Check if the jQuery object contains any element before you try to get its offset:

    var nav = $('.content-nav');
    if (nav.length) {
      var contentNav = nav.offset().top;
      ...continue to set up the menu
    }
    

提交回复
热议问题