LoginSignup
50
44

More than 5 years have passed since last update.

jQueryで取得される値から 'px' を取り除く方法

Posted at

jQueryでDOMのcssプロパティを取得した場合、"80px" というように、
pxまで含まれた文字列となっているため、今まで数値計算する際は、

replace('px', '')

という風に半ば強引にpxを消してから計算してた。
でも parseIntメソッドを使えば数値部分だけ切り出して返してくれるということを知った。

marginLeftVal = parseInt( $(elm).css('margin-left') );
50
44
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
50
44