最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nullpointerexception - JavaScript - Cannot read property 'top' of null - Stack Overflow

programmeradmin3浏览0评论

I have function that scrolls to a anchor position in a list

   function snapToAnchor(anchor) {
    $('#divProductScroll').scrollTop(0);
    var offset = $(anchor).offset().top - $('#divProductScroll').offset().top;
    $('#divProductScroll').scrollTop(offset);
    //$('#divProductScroll').animate({ scrollTop: offset }, 250);
}

but this is giving me error some times

saying Cannot read property 'top' of null

I am not so good with JavaScript

Can any one help with this issue?

I found the Problem.


This snapToAnchor function is in a dialog model, so second time when i hit this function, there is not list generated, that's why i have null value, so what i did is before firing this function, i recreate the modal and then step in to the function, now no scope of null.

I have function that scrolls to a anchor position in a list

   function snapToAnchor(anchor) {
    $('#divProductScroll').scrollTop(0);
    var offset = $(anchor).offset().top - $('#divProductScroll').offset().top;
    $('#divProductScroll').scrollTop(offset);
    //$('#divProductScroll').animate({ scrollTop: offset }, 250);
}

but this is giving me error some times

saying Cannot read property 'top' of null

I am not so good with JavaScript

Can any one help with this issue?

I found the Problem.


This snapToAnchor function is in a dialog model, so second time when i hit this function, there is not list generated, that's why i have null value, so what i did is before firing this function, i recreate the modal and then step in to the function, now no scope of null.

Share Improve this question edited Oct 28, 2011 at 20:07 HaBo asked Oct 28, 2011 at 18:56 HaBoHaBo 14.3k39 gold badges118 silver badges214 bronze badges 3
  • 2 I'm guessing you're not passing a legitimate tag name to the function in the anchor parameter. – Jonathan M Commented Oct 28, 2011 at 18:58
  • one of those two .offset() calls is returning a null. Split up the substraction and assign each offset to a var, and see which one es out null. – Marc B Commented Oct 28, 2011 at 18:59
  • could learn how to use firebug, then put a break inside the function, so you can see what's in the variable anchor – yitwail Commented Oct 28, 2011 at 19:03
Add a ment  | 

3 Answers 3

Reset to default 2

The anchor variable does not contain a valid selector for you site. Calling offset on this will return null.

Example

var a = $('#non-existing-id'); //returns empty object
var b = a.offset(); // returns null
b.top; //throws a TypeError exception

You can simply debug your program by inserting alert(anchor) on line 3. This will show you the contents of the variable.

if anchor has id of the DOM element - then correct way to use it with jQuery:

$('#' + anchor).offset()

otherwise you'll get a null (and error).

$(anchor).offset().top

will usually output a string not a number... something like 100px. you need to extract the number from the string.

var topoffset = $(anchor).offset().top;
topoffset = topoffset.split('px', 1);
发布评论

评论列表(0)

  1. 暂无评论