Add javascript pixel values?

前端 未结 5 486
甜味超标
甜味超标 2021-02-05 14:45

Is there a way in javascript/jQuery to take two variables with values \"60px\" and \"40px\" and add them together to get \"100px\"?

Or in a more general sense, I\'m tryi

5条回答
  •  情歌与酒
    2021-02-05 15:21

    Probably the most efficient way is

    parseInt("40px", 10) + parseInt("60px", 10) + "px"
    

    The 10's in the parseInt calls are necessary to protect against the value being mistakenly calculated in something other than base 10.

提交回复
热议问题