JS gets the pixel value without units

Source: Internet
Author: User

The so-called acquisition of pixel values without units is obtained such as the width of the element, height, font size, margin, padding equivalent but minus the pixel units.

For example: the width of an element is 100px, now I want to get this value but without the unit "PX", how do you solve this problem? My solution is to:

For example, by clicking on a button to get the value, you should write a function at this point, and then call the function when the button is clicked.

Function par (str) {

var num = str.replace ("px", "");

var num = parseint (num);

Alert (NUM)

}

Here, str.replace ("px", "") means to replace the PX in the string with a null, that is, to remove the PX, but even if the PX is removed, the value is still a string type, so now to do is to convert the string type to a numeric type, here I use the parseint method.

Now you can write it by clicking Execute with the button.

<input type= "button" onclick= "par (' 100px ')" value= "Click to get pixels without units" >

JS gets the pixel value without units

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.