To convert css values to numeric values, discard parseInt.

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.