9
10

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.

jQuery.fn.cssで複数のプロパティに取得する場合1.9からは配列指定した方がいい

Last updated at Posted at 2013-10-02

jQuery.fn.css はHTML要素の任意のCSSプロパティの値を取得します。

$('#sample').css('padding-left');  // => #sample 要素の padding-left の値が返される

1.以前は jQuery.fn.css は一度に1つのプロパティしか取得できませんでしたが、1.9からは配列を渡すことで一度に取得することができます。

var props = $('#sample').css(['padding-left', 'padding-right']);
props['padding-left'];  // => #sample の padding-left
props['padding-right'];  // => #sample の padding-right

パフォーマンス

jQuery.fn.css は内部的に window.getComputedStyle を使っています。配列を使った指定の場合 jQuery のメソッド呼び出しの回数が抑制されることに加えて、内部関数の実行回数も抑制されるのでパフォーマンスが良いようです。

9
10
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
9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?