6
6

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はChromeとFirefoxで微妙に挙動が異なる

Posted at

以下のようなHTML要素があるとします

<div id="sample" style="padding: 1px 2px 3px 4px;"></div>

これに対して、以下のようなJavaScriptを実行してみると返される値がChromeとFirefoxで微妙に異なります。

$('#sample').css('padding');
Chrome
"1px 2px 3px 4px"
Firefox
""

これは padding だけじゃなく、 marginbackground などのように - をまたいで複数のCSSを一行で設定するやつ(おそらく)全てに共通する挙動です。

回避方法

純粋に全て列挙します。

$('#sample').css(['padding-top', 'padding-bottom', 'padding-left', 'padding-right']);

この場合はChromeとFirefoxで挙動が一致します。

合わせて読みたい

6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?