Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 18:00:29
Original
1001 people have browsed it

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:


Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template