say I write the following javascript code:
var top = document.getElementById("SOMEDIVID").style.top;
Would the variable "top" end up storing the 开发者_开发知识库top value as a string or as a number? I want it to be a number.
It is a complex value - a number followed by a unit.
If a unit is missing, it is assumed to be pixels (px).
So, all of these are valid:
80px
80
50%
17em
use parseInt() then
var top = parseInt(document.getElementById("SOMEDIVID").style.top);
It would be a string. You can split off the integer with regular expression then a recast.
parseInt(top.match(/^\d+/)[0]); // integer only
This will tell you if it's a string or a number:
var type = typeof( top );
If it's a string, this will give you a number
var number = parseInt( top );
精彩评论