Home  >  Article  >  Web Front-end  >  Please abandon the parseInt_javascript technique when converting css values ​​into numerical values.

Please abandon the parseInt_javascript technique when converting css values ​​into numerical values.

WBOY
WBOYOriginal
2016-05-16 18:00:29998browse

For example:

Copy code The code is as follows:


<script> <br>var elem = document.getElementById('demo'), <br>width = elem.style.width; <br>alert( parseInt(width));// 24 <br></script>

But what if the css value is like this:
Copy Code The code is as follows:


var elem = document.getElementById('demo'),
opacity = elem.style.opacity;
alert(parseInt(opacity));// 0


It is obvious that parseInt() will make an error, even if it is a value like '.5', if you change it to parseFloat(), you will get the correct result:
Copy code The code is as follows: