Cross browser issue with offset() jquery function

后端 未结 2 1294
-上瘾入骨i
-上瘾入骨i 2020-12-18 01:08

I am having a cross browser issue with the offset() function in jQuery. For example, I am looking for the offset of an anchor tag

eg. $(\'#anchori

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 01:50

    I get this problem in IE8 when my script is loaded on a page where the element that we want to get the offset().top of does not exist.

    I solved it like this:

    if ($('#element').length){
        $('#element').offset().top // ...
    }
    

    Never execute offset().top if the element does not exist.

提交回复
热议问题