2
4

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.

load直後にjQueryを使ってcss属性値を取得できないことへの対処法

2
Last updated at Posted at 2016-02-02

loadイベント発生直後には、ブラウザが各HTML要素とCSS属性を結び付けられていないらしく、css属性値を取得できないことがある。

そこで、各要素とCSSがブラウザによって関連付けられるまで待つためにsetTimeoutを使い、loadイベント発生後に200ミリ秒ほど待ってからcss属性値を取得する。

var timer = false; /* グローバル変数 */
$(window).on("load resize", function() {
    if(timer !== false){
        clearTimeout(timer);
    }
    timer = setTimeout(function() {
         
        // jQueryを使ってcss属性値などを読み出す。
         
    }, 200);
});

2
4
0

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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?