更新时间:2023-02-26 09:32:52
parseInt()
非常可爱。
HTML
<span id="foo">280ms</span>
JS
var text = $('#foo').text();
var number = parseInt(text, 10);
alert(number);
parseInt()
将处理任何字符串一个数字,当它到达一个非数字字符时停止。在这种情况下, m
在 280ms
中。找到数字 2
, 8
, 0
,将这些数字计算为基数10(第二个参数)并返回数值 280
。请注意,这是一个实际数字,而不是字符串。
parseInt()
will process any string as a number and stop when it reaches a non-numeric character. In this case the the m
in 280ms
. After have found the digits 2
, 8
, and 0
, evaluates those digits as base 10 (that second argument) and returns the number value 280
. Note this is an actual number and not a string.
编辑
@Alex Wayne 的评论。
首先过滤掉非数字字符。
@Alex Wayne's comment.
Just filter out the non numeric characters first.
parseInt('ms120'.replace(/[^0-9\.]/g, ''), 10);