Get css top value as number not as string?

后端 未结 3 1548
长情又很酷
长情又很酷 2021-01-30 02:15

In jQuery you can get the top position relative to the parent as a number, but you can not get the css top value as a number if it was set in px.
Say I have th

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 02:41

    You can use the parseInt() function to convert the string to a number, e.g:

    parseInt($('#elem').css('top'));
    

    Update: (as suggested by Ben): You should give the radix too:

    parseInt($('#elem').css('top'), 10);
    

    Forces it to be parsed as a decimal number, otherwise strings beginning with '0' might be parsed as an octal number (might depend on the browser used).

提交回复
热议问题