2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

存在しない要素に対して.offset().topするとエラーになる

Posted at

topに限った話ではありませんが、以下のようにするとエラーになります。

$('.notExistElem').offset().top

これはHTMLに要素を記述していない場合だけでなく、display: noneの場合にも起こるようです(visibility: hiddenは大丈夫)。

エラーを防ぐためにはif文などで要素の存在を調べてから処理しましょう(.lengthなどを使うと良いです)。

2
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?