For example:
Copy codeThe Code is as follows: <div id = "demo" style = "width: 24px"> </div>
<Script>
Var elem = document. getElementById ('Demo '),
Width = elem. style. width;
Alert (parseInt (width); // 24
</Script>
However, if the css value is like this:Copy codeThe Code is as follows: <div id = "demo" style = "opacity: 0.5"> </div>
<Script>
Var elem = document. getElementById ('Demo '),
Opacity = elem. style. opacity;
Alert (parseInt (opacity); // 0
</Script>
Apparently, the above parseInt () will cause an error, even if it is a value like '. 5', if it is changed to parseFloat (), the correct result will be obtained:Copy codeThe Code is as follows: <div id = "demo" style = "opacity: 0.5"> </div>
<Script>
Var elem = document. getElementById ('Demo '),
Opacity = elem. style. opacity;
Alert (parseFloat (opacity); // 0.5
What is the difference between the two?
ParseInt () returns an integer.
ParseFloat () returns a floating point number.
Learn more about parseInt and parseFloat